forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add reader for SPSS (.sav) files (pandas-dev#26537)
- Loading branch information
Showing
18 changed files
with
161 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
YEAR: 2013-2016 | ||
COPYRIGHT HOLDER: Hadley Wickham; RStudio; and Evan Miller |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
Based on http://opensource.org/licenses/MIT | ||
|
||
This is a template. Complete and ship as file LICENSE the following 2 | ||
lines (only) | ||
|
||
YEAR: | ||
COPYRIGHT HOLDER: | ||
|
||
and specify as | ||
|
||
License: MIT + file LICENSE | ||
|
||
Copyright (c) <YEAR>, <COPYRIGHT HOLDER> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ dependencies: | |
- xlsxwriter | ||
- xlwt | ||
- pip: | ||
- pyreadstat | ||
# universal | ||
- pytest==4.5.0 | ||
- pytest-xdist | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,4 @@ dependencies: | |
- pytest-mock | ||
- moto | ||
- hypothesis>=3.58.0 | ||
- pyreadstat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,5 +19,6 @@ dependencies: | |
- hypothesis>=3.58.0 | ||
- s3fs | ||
- pip | ||
- pyreadstat | ||
- pip: | ||
- moto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from pathlib import Path | ||
from typing import Optional, Sequence, Union | ||
|
||
from pandas.compat._optional import import_optional_dependency | ||
|
||
from pandas.api.types import is_list_like | ||
from pandas.core.api import DataFrame | ||
|
||
|
||
def read_spss(path: Union[str, Path], | ||
usecols: Optional[Sequence[str]] = None, | ||
convert_categoricals: bool = True) -> DataFrame: | ||
""" | ||
Load an SPSS file from the file path, returning a DataFrame. | ||
.. versionadded 0.25.0 | ||
Parameters | ||
---------- | ||
path : string or Path | ||
File path | ||
usecols : list-like, optional | ||
Return a subset of the columns. If None, return all columns. | ||
convert_categoricals : bool, default is True | ||
Convert categorical columns into pd.Categorical. | ||
Returns | ||
------- | ||
DataFrame | ||
""" | ||
pyreadstat = import_optional_dependency("pyreadstat") | ||
|
||
if usecols is not None: | ||
if not is_list_like(usecols): | ||
raise TypeError("usecols must be list-like.") | ||
else: | ||
usecols = list(usecols) # pyreadstat requires a list | ||
|
||
df, _ = pyreadstat.read_sav(path, usecols=usecols, | ||
apply_value_formats=convert_categoricals) | ||
return df |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
import pandas as pd | ||
from pandas.util import testing as tm | ||
|
||
pyreadstat = pytest.importorskip("pyreadstat") | ||
|
||
|
||
def test_spss_labelled_num(datapath): | ||
# test file from the Haven project (https://haven.tidyverse.org/) | ||
fname = datapath("io", "data", "labelled-num.sav") | ||
|
||
df = pd.read_spss(fname, convert_categoricals=True) | ||
expected = pd.DataFrame({"VAR00002": "This is one"}, index=[0]) | ||
expected["VAR00002"] = pd.Categorical(expected["VAR00002"]) | ||
tm.assert_frame_equal(df, expected) | ||
|
||
df = pd.read_spss(fname, convert_categoricals=False) | ||
expected = pd.DataFrame({"VAR00002": 1.0}, index=[0]) | ||
tm.assert_frame_equal(df, expected) | ||
|
||
|
||
def test_spss_labelled_num_na(datapath): | ||
# test file from the Haven project (https://haven.tidyverse.org/) | ||
fname = datapath("io", "data", "labelled-num-na.sav") | ||
|
||
df = pd.read_spss(fname, convert_categoricals=True) | ||
expected = pd.DataFrame({"VAR00002": ["This is one", None]}) | ||
expected["VAR00002"] = pd.Categorical(expected["VAR00002"]) | ||
tm.assert_frame_equal(df, expected) | ||
|
||
df = pd.read_spss(fname, convert_categoricals=False) | ||
expected = pd.DataFrame({"VAR00002": [1.0, np.nan]}) | ||
tm.assert_frame_equal(df, expected) | ||
|
||
|
||
def test_spss_labelled_str(datapath): | ||
# test file from the Haven project (https://haven.tidyverse.org/) | ||
fname = datapath("io", "data", "labelled-str.sav") | ||
|
||
df = pd.read_spss(fname, convert_categoricals=True) | ||
expected = pd.DataFrame({"gender": ["Male", "Female"]}) | ||
expected["gender"] = pd.Categorical(expected["gender"]) | ||
tm.assert_frame_equal(df, expected) | ||
|
||
df = pd.read_spss(fname, convert_categoricals=False) | ||
expected = pd.DataFrame({"gender": ["M", "F"]}) | ||
tm.assert_frame_equal(df, expected) | ||
|
||
|
||
def test_spss_umlauts(datapath): | ||
# test file from the Haven project (https://haven.tidyverse.org/) | ||
fname = datapath("io", "data", "umlauts.sav") | ||
|
||
df = pd.read_spss(fname, convert_categoricals=True) | ||
expected = pd.DataFrame({"var1": ["the ä umlaut", | ||
"the ü umlaut", | ||
"the ä umlaut", | ||
"the ö umlaut"]}) | ||
expected["var1"] = pd.Categorical(expected["var1"]) | ||
tm.assert_frame_equal(df, expected) | ||
|
||
df = pd.read_spss(fname, convert_categoricals=False) | ||
expected = pd.DataFrame({"var1": [1.0, 2.0, 1.0, 3.0]}) | ||
tm.assert_frame_equal(df, expected) | ||
|
||
|
||
def test_spss_usecols(datapath): | ||
# usecols must be list-like | ||
fname = datapath("io", "data", "labelled-num.sav") | ||
|
||
with pytest.raises(TypeError, match="usecols must be list-like."): | ||
pd.read_spss(fname, usecols="VAR00002") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,4 +52,5 @@ sqlalchemy | |
xarray | ||
xlrd | ||
xlsxwriter | ||
xlwt | ||
xlwt | ||
pyreadstat |