|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | +import pandas as pd |
| 5 | +from pandas.util import testing as tm |
| 6 | + |
| 7 | +pyreadstat = pytest.importorskip("pyreadstat") |
| 8 | + |
| 9 | + |
| 10 | +def test_spss_labelled_num(datapath): |
| 11 | + # test file from the Haven project (https://haven.tidyverse.org/) |
| 12 | + fname = datapath("io", "data", "labelled-num.sav") |
| 13 | + |
| 14 | + df = pd.read_spss(fname, convert_categoricals=True) |
| 15 | + expected = pd.DataFrame({"VAR00002": "This is one"}, index=[0]) |
| 16 | + expected["VAR00002"] = pd.Categorical(expected["VAR00002"]) |
| 17 | + tm.assert_frame_equal(df, expected) |
| 18 | + |
| 19 | + df = pd.read_spss(fname, convert_categoricals=False) |
| 20 | + expected = pd.DataFrame({"VAR00002": 1.0}, index=[0]) |
| 21 | + tm.assert_frame_equal(df, expected) |
| 22 | + |
| 23 | + |
| 24 | +def test_spss_labelled_num_na(datapath): |
| 25 | + # test file from the Haven project (https://haven.tidyverse.org/) |
| 26 | + fname = datapath("io", "data", "labelled-num-na.sav") |
| 27 | + |
| 28 | + df = pd.read_spss(fname, convert_categoricals=True) |
| 29 | + expected = pd.DataFrame({"VAR00002": ["This is one", None]}) |
| 30 | + expected["VAR00002"] = pd.Categorical(expected["VAR00002"]) |
| 31 | + tm.assert_frame_equal(df, expected) |
| 32 | + |
| 33 | + df = pd.read_spss(fname, convert_categoricals=False) |
| 34 | + expected = pd.DataFrame({"VAR00002": [1.0, np.nan]}) |
| 35 | + tm.assert_frame_equal(df, expected) |
| 36 | + |
| 37 | + |
| 38 | +def test_spss_labelled_str(datapath): |
| 39 | + # test file from the Haven project (https://haven.tidyverse.org/) |
| 40 | + fname = datapath("io", "data", "labelled-str.sav") |
| 41 | + |
| 42 | + df = pd.read_spss(fname, convert_categoricals=True) |
| 43 | + expected = pd.DataFrame({"gender": ["Male", "Female"]}) |
| 44 | + expected["gender"] = pd.Categorical(expected["gender"]) |
| 45 | + tm.assert_frame_equal(df, expected) |
| 46 | + |
| 47 | + df = pd.read_spss(fname, convert_categoricals=False) |
| 48 | + expected = pd.DataFrame({"gender": ["M", "F"]}) |
| 49 | + tm.assert_frame_equal(df, expected) |
| 50 | + |
| 51 | + |
| 52 | +def test_spss_umlauts(datapath): |
| 53 | + # test file from the Haven project (https://haven.tidyverse.org/) |
| 54 | + fname = datapath("io", "data", "umlauts.sav") |
| 55 | + |
| 56 | + df = pd.read_spss(fname, convert_categoricals=True) |
| 57 | + expected = pd.DataFrame({"var1": ["the ä umlaut", |
| 58 | + "the ü umlaut", |
| 59 | + "the ä umlaut", |
| 60 | + "the ö umlaut"]}) |
| 61 | + expected["var1"] = pd.Categorical(expected["var1"]) |
| 62 | + tm.assert_frame_equal(df, expected) |
| 63 | + |
| 64 | + df = pd.read_spss(fname, convert_categoricals=False) |
| 65 | + expected = pd.DataFrame({"var1": [1.0, 2.0, 1.0, 3.0]}) |
| 66 | + tm.assert_frame_equal(df, expected) |
| 67 | + |
| 68 | + |
| 69 | +def test_spss_usecols(datapath): |
| 70 | + # usecols must be list-like |
| 71 | + fname = datapath("io", "data", "labelled-num.sav") |
| 72 | + |
| 73 | + with pytest.raises(TypeError, match="usecols must be list-like."): |
| 74 | + pd.read_spss(fname, usecols="VAR00002") |
0 commit comments