Pyreadstat is a Python library that allows reading and writing SAS (.sas7bdat, .xpt), SPSS (.sav, .zsav), and Stata (.dta) files into/from pandas and polars data frames. It is currently at version 1.3.3 and maintains an active, though irregular, release cadence to adapt to new pandas/polars versions and add features.
pip install pyreadstatVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to import pyreadstat, write a simple pandas DataFrame to a SAS .sas7bdat file, and then read it back, illustrating basic data and metadata retrieval. The same `read_*` and `write_*` patterns apply to SPSS and Stata files.
Upgrade pyreadstat to version 1.3.3 or newer to ensure compatibility with pandas 3.0. If you cannot upgrade pyreadstat, consider pinning your pandas version to `<3.0`.
Use the `encoding` parameter in `read_sas7bdat()`, `read_sav()`, or `read_dta()`. Common encodings include 'latin-1', 'cp1252', 'utf-8'. Example: `pyreadstat.read_sas7bdat('file.sas7bdat', encoding='latin-1')`.Ensure your system has sufficient RAM to accommodate the file size. For extremely large files, consider preprocessing them with other tools (e.g., SAS/SPSS/Stata itself) to extract subsets, or convert them to more memory-efficient formats (like Parquet or Feather) before processing with Python if only specific columns/rows are needed.
If SAS date/time variables are not parsed correctly, inspect the metadata (`meta.variable_formats`) and consider passing `sas_time_is_datetime_format=True` to `read_sas7bdat` if the SAS file explicitly stores datetime types. Otherwise, manual conversion might be necessary post-read.
Install the library using pip: `pip install pyreadstat`
Explicitly specify the correct encoding (e.g., `'latin1'`, `'cp1252'`, `'utf-8'`) using the `encoding` parameter in the `read_sas`, `read_spss`, or `read_stata` function.
Verify the exact file path and name, ensure the file exists at the specified location, and check that the user running the script has appropriate read permissions for the file.
Download and install the 'Build Tools for Visual Studio' from Microsoft's website, ensuring that the 'Desktop development with C++' workload is selected during installation.