Skip to content

Commit de043c2

Browse files
committed
BUG: Ensure read_spss accepts pathlib Paths (GH33666)
1 parent 6aa311d commit de043c2

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

doc/source/whatsnew/v1.1.3.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Fixed regressions
2222

2323
Bug fixes
2424
~~~~~~~~~
25-
-
25+
- Bug in :func:`read_spss` where passing a ``pathlib.Path`` as ``path`` would raise a ``TypeError`` (:issue:`33666`)
2626

2727
.. ---------------------------------------------------------------------------
2828

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: 5 additions & 2 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

@@ -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)