Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/pandas/io/spss.py: 41%

16 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1from __future__ import annotations 

2 

3from pathlib import Path 

4from typing import ( 

5 TYPE_CHECKING, 

6 Sequence, 

7) 

8 

9from pandas.compat._optional import import_optional_dependency 

10 

11from pandas.core.dtypes.inference import is_list_like 

12 

13from pandas.io.common import stringify_path 

14 

15if TYPE_CHECKING: 15 ↛ 16line 15 didn't jump to line 16, because the condition on line 15 was never true

16 from pandas import DataFrame 

17 

18 

19def read_spss( 

20 path: str | Path, 

21 usecols: Sequence[str] | None = None, 

22 convert_categoricals: bool = True, 

23) -> DataFrame: 

24 """ 

25 Load an SPSS file from the file path, returning a DataFrame. 

26 

27 .. versionadded:: 0.25.0 

28 

29 Parameters 

30 ---------- 

31 path : str or Path 

32 File path. 

33 usecols : list-like, optional 

34 Return a subset of the columns. If None, return all columns. 

35 convert_categoricals : bool, default is True 

36 Convert categorical columns into pd.Categorical. 

37 

38 Returns 

39 ------- 

40 DataFrame 

41 """ 

42 pyreadstat = import_optional_dependency("pyreadstat") 

43 

44 if usecols is not None: 

45 if not is_list_like(usecols): 

46 raise TypeError("usecols must be list-like.") 

47 else: 

48 usecols = list(usecols) # pyreadstat requires a list 

49 

50 df, _ = pyreadstat.read_sav( 

51 stringify_path(path), usecols=usecols, apply_value_formats=convert_categoricals 

52 ) 

53 return df