Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pandas/io/spss.py: 45%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from __future__ import annotations
3from pathlib import Path
4from typing import (
5 TYPE_CHECKING,
6 Sequence,
7)
9from pandas._libs import lib
10from pandas.compat._optional import import_optional_dependency
11from pandas.util._validators import check_dtype_backend
13from pandas.core.dtypes.inference import is_list_like
15from pandas.io.common import stringify_path
17if TYPE_CHECKING:
18 from pandas._typing import DtypeBackend
20 from pandas import DataFrame
23def read_spss(
24 path: str | Path,
25 usecols: Sequence[str] | None = None,
26 convert_categoricals: bool = True,
27 dtype_backend: DtypeBackend | lib.NoDefault = lib.no_default,
28) -> DataFrame:
29 """
30 Load an SPSS file from the file path, returning a DataFrame.
32 Parameters
33 ----------
34 path : str or Path
35 File path.
36 usecols : list-like, optional
37 Return a subset of the columns. If None, return all columns.
38 convert_categoricals : bool, default is True
39 Convert categorical columns into pd.Categorical.
40 dtype_backend : {"numpy_nullable", "pyarrow"}, defaults to NumPy backed DataFrames
41 Which dtype_backend to use, e.g. whether a DataFrame should have NumPy
42 arrays, nullable dtypes are used for all dtypes that have a nullable
43 implementation when "numpy_nullable" is set, pyarrow is used for all
44 dtypes if "pyarrow" is set.
46 The dtype_backends are still experimential.
48 .. versionadded:: 2.0
50 Returns
51 -------
52 DataFrame
53 """
54 pyreadstat = import_optional_dependency("pyreadstat")
55 check_dtype_backend(dtype_backend)
57 if usecols is not None:
58 if not is_list_like(usecols):
59 raise TypeError("usecols must be list-like.")
60 usecols = list(usecols) # pyreadstat requires a list
62 df, _ = pyreadstat.read_sav(
63 stringify_path(path), usecols=usecols, apply_value_formats=convert_categoricals
64 )
65 if dtype_backend is not lib.no_default:
66 df = df.convert_dtypes(dtype_backend=dtype_backend)
67 return df