How do I get Pandas to read SPSS files?

Viewed 1980

I tried using the read_SPSS command, but it didn't work. The error seems to be telling me that I need pyreadstat, but I'm unable to even install that. Why, though, would I need pyreadstat? Isn't the ability to read .sav just built into Pandas these days? I'm confused...

Here's the error text I get when I try using read_SPSS:


ImportError                               Traceback (most recent call last)
<ipython-input-33-935d54be8b7a> in <module>
----> 1 twitter=pd.read_spss('twitter_study.sav')

/opt/anaconda3/lib/python3.8/site-packages/pandas/io/spss.py in read_spss(path, usecols, convert_categoricals)
     34     DataFrame
     35     """
---> 36     pyreadstat = import_optional_dependency("pyreadstat")
     37 
     38     if usecols is not None:

/opt/anaconda3/lib/python3.8/site-packages/pandas/compat/_optional.py in import_optional_dependency(name, extra, raise_on_missing, on_version)
    108     except ImportError:
    109         if raise_on_missing:
--> 110             raise ImportError(msg) from None
    111         else:
    112             return None

ImportError: Missing optional dependency 'pyreadstat'.  Use pip or conda to install pyreadstat.
3 Answers

What is built into pandas.read_spss() can be seen here.

The error message you are getting is a bit misleading as it suggests pyreadstat is optional, but the source code shows that pandas.read_spss() relies entirely on pyreadstat.

The easiest way to solve this would therefore be to install pyreadstat.

For those who are reading now, use

conda install -c conda-forge pyreadstat

it works well.

I had the same issue, couldn't pip install or conda install, you can't find it through Anaconda Navigator and it's not listed here https://repo.anaconda.com/pkgs/main/

It got solved by running the following command mentioned by @埃塞ABELA in the conda powershell prompt

conda install -c conda-forge pyreadstat
Related