Skip to content

Commit 3ba49d6

Browse files
committed
BUG: Ensure read_spss accepts pathlib Paths (GH33666)
1 parent aca77f7 commit 3ba49d6

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

doc/source/whatsnew/v1.1.2.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Bug fixes
4141
- Bug in :meth:`Series.dt.isocalendar` and :meth:`DatetimeIndex.isocalendar` that returned incorrect year for certain dates (:issue:`36032`)
4242
- Bug in :class:`DataFrame` indexing returning an incorrect :class:`Series` in some cases when the series has been altered and a cache not invalidated (:issue:`33675`)
4343
- Bug in :meth:`DataFrame.corr` causing subsequent indexing lookups to be incorrect (:issue:`35882`)
44+
- Bug in :func:`read_spss` where passing a ``pathlib.Path`` as ``path`` would raise a ``TypeError`` (:issue:`33666`)
4445

4546
.. ---------------------------------------------------------------------------
4647
@@ -50,6 +51,7 @@ Other
5051
~~~~~
5152
- :meth:`factorize` now supports ``na_sentinel=None`` to include NaN in the uniques of the values and remove ``dropna`` keyword which was unintentionally exposed to public facing API in 1.1 version from :meth:`factorize` (:issue:`35667`)
5253

54+
5355
.. ---------------------------------------------------------------------------
5456
5557
.. _whatsnew_112.contributors:

pandas/io/spss.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
from pandas.core.api import DataFrame
99

10+
from pandas.io.common import stringify_path
11+
1012

1113
def read_spss(
1214
path: Union[str, Path],
@@ -40,6 +42,6 @@ def read_spss(
4042
usecols = list(usecols) # pyreadstat requires a list
4143

4244
df, _ = pyreadstat.read_sav(
43-
path, usecols=usecols, apply_value_formats=convert_categoricals
45+
stringify_path(path), usecols=usecols, apply_value_formats=convert_categoricals
4446
)
4547
return df

pandas/tests/io/test_spss.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from pathlib import Path
2+
13
import numpy as np
24
import pytest
35

@@ -71,3 +73,13 @@ def test_spss_usecols(datapath):
7173

7274
with pytest.raises(TypeError, match="usecols must be list-like."):
7375
pd.read_spss(fname, usecols="VAR00002")
76+
77+
78+
def test_can_read_spss_file_from_local_path_as_string(datapath):
79+
sav_path = datapath("io", "data", "spss", "labelled-num.sav")
80+
pd.read_spss(sav_path)
81+
82+
83+
def test_can_read_spss_file_from_local_path_as_pathlib_path(datapath):
84+
sav_path = Path(datapath("io", "data", "spss", "labelled-num.sav"))
85+
pd.read_spss(sav_path)

0 commit comments

Comments
 (0)