Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1from pathlib import Path 

2from typing import Optional, Sequence, Union 

3 

4from pandas.compat._optional import import_optional_dependency 

5 

6from pandas.core.dtypes.inference import is_list_like 

7 

8from pandas.core.api import DataFrame 

9 

10 

11def read_spss( 

12 path: Union[str, Path], 

13 usecols: Optional[Sequence[str]] = None, 

14 convert_categoricals: bool = True, 

15) -> DataFrame: 

16 """ 

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

18 

19 .. versionadded:: 0.25.0 

20 

21 Parameters 

22 ---------- 

23 path : string or Path 

24 File path. 

25 usecols : list-like, optional 

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

27 convert_categoricals : bool, default is True 

28 Convert categorical columns into pd.Categorical. 

29 

30 Returns 

31 ------- 

32 DataFrame 

33 """ 

34 pyreadstat = import_optional_dependency("pyreadstat") 

35 

36 if usecols is not None: 

37 if not is_list_like(usecols): 

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

39 else: 

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

41 

42 df, _ = pyreadstat.read_sav( 

43 path, usecols=usecols, apply_value_formats=convert_categoricals 

44 ) 

45 return df