Skip to content

Commit 69ff179

Browse files
authored
BUG: Ensure read_spss accepts pathlib Paths (GH33666) (#36174)
1 parent 00353d5 commit 69ff179

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

doc/source/whatsnew/v1.1.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Fixed regressions
2626

2727
Bug fixes
2828
~~~~~~~~~
29+
- Bug in :func:`read_spss` where passing a ``pathlib.Path`` as ``path`` would raise a ``TypeError`` (:issue:`33666`)
2930
- Bug in :meth:`Series.str.startswith` and :meth:`Series.str.endswith` with ``category`` dtype not propagating ``na`` parameter (:issue:`36241`)
3031
- Bug in :class:`Series` constructor where integer overflow would occur for sufficiently large scalar inputs when an index was provided (:issue:`36291`)
3132

pandas/io/spss.py

+3-1
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

+5-2
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

@@ -7,9 +9,10 @@
79
pyreadstat = pytest.importorskip("pyreadstat")
810

911

10-
def test_spss_labelled_num(datapath):
12+
@pytest.mark.parametrize("path_klass", [lambda p: p, Path])
13+
def test_spss_labelled_num(path_klass, datapath):
1114
# test file from the Haven project (https://haven.tidyverse.org/)
12-
fname = datapath("io", "data", "spss", "labelled-num.sav")
15+
fname = path_klass(datapath("io", "data", "spss", "labelled-num.sav"))
1316

1417
df = pd.read_spss(fname, convert_categoricals=True)
1518
expected = pd.DataFrame({"VAR00002": "This is one"}, index=[0])

0 commit comments

Comments
 (0)