Package: pyspssio
Overviewpyspssio is a python package for reading and writing SPSS (.sav and .zsav) files to/from pandas dataframes.This package uses the I/O Module for SPSS Statistics v27 available athttps://www.ibm.com/.WARNING: This is an early release with limited testing. Use with caution.LinksPyPIGitHubRead the DocsMotivationMain reason for creating this package is to fill gaps by other similar packages.savReaderWriterdoesn't support python > 3.5not particularly user friendlypyreadstatdoesn't read or write multi response set definitionsdatetime conversion quirksissues reading/writing long string variables (https://github.com/Roche/pyreadstat/issues/119)pyspssiosupports recent versions of python and can read/write most SPSS file metadata properties. Theusecolsargument when reading files also accepts a callable for more flexible variable selection.Basic UsageInstallationpip install pyspssioImportimport pyspssioReadingRead data and metadatadf, meta = pyspssio.read_sav("spss_file.sav")Read metadata onlymeta = pyspssio.read_metadata("spss_file.sav")Read data in chunks ofchunksize(number of rows/records)for df in pyspssio.read_sav("spss_file.sav", chunksize=1000):
#   do somethingNote: metadata is not returned when reading in chunksWritingWrite dataframe to file.pyspssio.write_sav("spss_file.sav", df)AppendingAppend existing SPSS file with new records.pyspssio.write_sav("spss_file.sav", df)Note: Cannot modify metadata when appending new records. Be careful with strings that might be longer than the already defined width as they may be automatically truncated without warning.Other NotesDate/Time VariablesDate and datetime variables- These are converted to/from full datetime objects, even for formats like DATE, QYR, and WKYR which don't display a time component. Users can opt to use Pandas'.dtaccessor to extract specific components or force a specific accuracy (e.g., minute, day, hour) after reading the data (ex..dt.floor). Thevar_formatsand/orvar_formats_tuplemetadata attributes can be used to see the original SPSS formats.Time variables- These are converted to/from timestamp objects.Python/Pandas stores datetimes in nanseconds while SPSS stores them in seconds. Due to conversions that must take place, there may be some small (ms) discrepancies between an original dataframe used to write an SPSS file and a dataframe read back from the same SPSS file.I/O Module ProceduresList of available I/O module procedures and class for which they fall under. See official documentation for details on each one.Some of these procedures are implemented as hidden methods referenced within a more generalized function/property. For example, instead of callingspssSetVarLabelmanually for each variable, users should assign all variable labels at once by settingself.var_labels = {var1: label1, var2: label2, ...}.All I/O module procedures can be accessed directly withself.spssio.[procedure].SPSSFilespssOpenReadspssCloseReadspssOpenWritespssCloseWritespssOpenAppendspssCloseAppendspssHostSysmisValspssLowHighValspssSetLocalespssGetInterfaceEncodingspssSetInterfaceEncodingspssGetFileEncodingspssIsCompatibleEncodingspssGetCompressionspssSetCompressionspssGetReleaseInfospssGetNumberofCasesspssGetNumberofVariablesHeaderspssGetFileAttributesspssSetFileAttributesspssGetVarNamesspssSetVarNamespssGetVarHandlespssGetVarPrintFormatspssSetVarPrintFormatspssSetVarWriteFormatspssGetVarMeasureLevelspssSetVarMeasureLevelspssGetVarAlignmentspssSetVarAlignmentspssGetVarColumnWidthspssSetVarColumnWidthspssGetVarLabelLongspssSetVarLabelspssGetVarRolespssSetVarRolespssGetVarCValueLabelsspssSetVarCValueLabelspssGetVarNValueLabelsspssSetVarNValueLabelspssGetVarCMissingValuesspssSetVarCMissingValuesspssGetVarNMissingValuesspssSetVarNMissingValuesspssGetMultRespCountspssGetMultRespDefsspssGetMultRespDefsEx - replaces spssGetMultRespDefsspssSetMultRespDefsspssAddMultRespDefExtspssGetCaseSizespssGetCaseWeightVarspssSetCaseWeightVarspssGetVarAttributesspssSetVarAttributesspssGetVarCompatNamespssGetVariableSetsspssSetVariableSetsspssCommitHeaderReaderspssSeekNextCasespssWholeCaseInWriterspssWholeCaseOutspssSetValueCharspssSetValueNumericspssCommitCaseRecordNot Implemented (yet)spssAddMultRespDefCspssAddMultRespDefNspssGetMultRespDefByIndexspssConvertDate - manual conversion insteadspssConvertSPSSDate - manual conversion insteadspssConvertSPSSTime - manual conversion insteadspssConvertTime - manual conversion insteadspssCopyDocumentsspssGetDEWFirstspssGetDEWGUIDspssGetDewInfospssGetDEWNextspssSetDEWFirstspssSetDEWGUIDspssSetDEWNextspssGetDateVariablesspssGetEstimatedNofCasesspssGetFileAttribute - uses spssGetFileAttributes insteadspssGetFileCodePagespssGetIdStringspssGetSystemStringspssGetTextInfospssGetTimeStampspssGetValueChar - uses spssWholeCaseIn insteadspssGetValueNumeric - uses spssWholeCaseIn insteadspssAddVarAttribute - uses spssSetVarAttributes insteadspssGetVarCValueLabel - uses spssGetVarCValueLabels insteadspssGetVarCValueLabelLong - uses spssGetVarCValueLabels insteadspssGetVarInfospssGetVarLabel - uses spssGetVarLabelLong insteadspssGetVarNValueLabel - uses spssGetVarNValueLabels insteadspssGetVarNValueLabelLong - uses spssGetVarNValueLabels insteadspssGetVarWriteFormat - uses spssGetVarPrintFormat instead (print/write formats tied together)spssOpenAppendExspssOpenReadExspssOpenWriteCopyspssOpenWriteCopyExspssOpenWriteCopyExDictspssOpenWriteCopyExFilespssOpenWriteExspssQueryType7spssReadCaseRecord - uses spssWholeCaseIn insteadspssSetDateVariablesspssSetIdStringspssSetTempDirspssSetTextInfospssSetVarCValueLabels - uses spssSetVarCValueLabel insteadspssSetVarNValueLabels - uses spssSetVarNValueLabel insteadspssSysmisVal - uses spssHostSysmisVal insteadspssValidateVarname