6
6
Sequence ,
7
7
)
8
8
9
+ from pandas ._config import using_nullable_dtypes
10
+
11
+ from pandas ._libs import lib
9
12
from pandas .compat ._optional import import_optional_dependency
10
13
11
14
from pandas .core .dtypes .inference import is_list_like
@@ -20,6 +23,7 @@ def read_spss(
20
23
path : str | Path ,
21
24
usecols : Sequence [str ] | None = None ,
22
25
convert_categoricals : bool = True ,
26
+ use_nullable_dtypes : bool | lib .NoDefault = lib .no_default ,
23
27
) -> DataFrame :
24
28
"""
25
29
Load an SPSS file from the file path, returning a DataFrame.
@@ -32,13 +36,33 @@ def read_spss(
32
36
Return a subset of the columns. If None, return all columns.
33
37
convert_categoricals : bool, default is True
34
38
Convert categorical columns into pd.Categorical.
39
+ use_nullable_dtypes : bool = False
40
+ Whether to use nullable dtypes as default when reading data. If
41
+ set to True, nullable dtypes are used for all dtypes that have a nullable
42
+ implementation, even if no nulls are present.
43
+
44
+ .. note::
45
+
46
+ The nullable dtype implementation can be configured by calling
47
+ ``pd.set_option("mode.dtype_backend", "pandas")`` to use
48
+ numpy-backed nullable dtypes or
49
+ ``pd.set_option("mode.dtype_backend", "pyarrow")`` to use
50
+ pyarrow-backed nullable dtypes (using ``pd.ArrowDtype``).
51
+
52
+ .. versionadded:: 2.0
35
53
36
54
Returns
37
55
-------
38
56
DataFrame
39
57
"""
40
58
pyreadstat = import_optional_dependency ("pyreadstat" )
41
59
60
+ use_nullable_dtypes = (
61
+ use_nullable_dtypes
62
+ if use_nullable_dtypes is not lib .no_default
63
+ else using_nullable_dtypes ()
64
+ )
65
+
42
66
if usecols is not None :
43
67
if not is_list_like (usecols ):
44
68
raise TypeError ("usecols must be list-like." )
@@ -47,4 +71,6 @@ def read_spss(
47
71
df , _ = pyreadstat .read_sav (
48
72
stringify_path (path ), usecols = usecols , apply_value_formats = convert_categoricals
49
73
)
74
+ if use_nullable_dtypes :
75
+ df = df .convert_dtypes ()
50
76
return df
0 commit comments