Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pandas/io/spss.py: 42%
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 typing import (
4 TYPE_CHECKING,
5 Any,
6)
8from pandas._libs import lib
9from pandas.compat._optional import import_optional_dependency
10from pandas.util._decorators import set_module
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 collections.abc import Sequence
19 from pathlib import Path
21 from pandas._typing import DtypeBackend
23 from pandas import DataFrame
26@set_module("pandas")
27def read_spss(
28 path: str | Path,
29 usecols: Sequence[str] | None = None,
30 convert_categoricals: bool = True,
31 dtype_backend: DtypeBackend | lib.NoDefault = lib.no_default,
32 **kwargs: Any,
33) -> DataFrame:
34 """
35 Load an SPSS file from the file path, returning a DataFrame.
37 Parameters
38 ----------
39 path : str or Path
40 File path.
41 usecols : list-like, optional
42 Return a subset of the columns. If None, return all columns.
43 convert_categoricals : bool, default is True
44 Convert categorical columns into pd.Categorical.
45 dtype_backend : {'numpy_nullable', 'pyarrow'}
46 Back-end data type applied to the resultant :class:`DataFrame`
47 (still experimental). If not specified, the default behavior
48 is to not use nullable data types. If specified, the behavior
49 is as follows:
51 * ``"numpy_nullable"``: returns nullable-dtype-backed :class:`DataFrame`
52 * ``"pyarrow"``: returns pyarrow-backed
53 nullable :class:`ArrowDtype` :class:`DataFrame`
55 .. versionadded:: 2.0
56 **kwargs
57 Additional keyword arguments that can be passed to :func:`pyreadstat.read_sav`.
59 .. versionadded:: 3.0
61 Returns
62 -------
63 DataFrame
64 DataFrame based on the SPSS file.
66 See Also
67 --------
68 read_csv : Read a comma-separated values (csv) file into a pandas DataFrame.
69 read_excel : Read an Excel file into a pandas DataFrame.
70 read_sas : Read an SAS file into a pandas DataFrame.
71 read_orc : Load an ORC object into a pandas DataFrame.
72 read_feather : Load a feather-format object into a pandas DataFrame.
74 Examples
75 --------
76 >>> df = pd.read_spss("spss_data.sav") # doctest: +SKIP
77 """
78 pyreadstat = import_optional_dependency("pyreadstat")
79 check_dtype_backend(dtype_backend)
81 if usecols is not None:
82 if not is_list_like(usecols):
83 raise TypeError("usecols must be list-like.")
84 usecols = list(usecols) # pyreadstat requires a list
86 df, metadata = pyreadstat.read_sav(
87 stringify_path(path),
88 usecols=usecols,
89 apply_value_formats=convert_categoricals,
90 **kwargs,
91 )
92 df.attrs = metadata.__dict__
93 if dtype_backend is not lib.no_default:
94 df = df.convert_dtypes(dtype_backend=dtype_backend)
95 return df