Programmer
Programmer

Reputation: 1294

Reading sas7bdat as pandas dataframe from zipfile

I have a zip file called myfile.zip, which contains a file mysasfile.sas7bdat, which I would like to read as a pandas dataframe. I've tried a few things which haven't worked, but here is my current methodology:

import zipfile

zipfile = zipfile.ZipFile('myfile.zip', 'r')
sasfile = zipfile.open('mysasfile.sas7bdat')

df = pd.read_sas(sasfile)

Error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-82-6d55436287b5> in <module>()
      3 imgfile = archive.open('curated_dataset_preview.sas7bdat')
      4 
----> 5 df = pd.read_sas(imgfile)

/opt/python/python35/lib/python3.5/site-packages/pandas/io/sas/sasreader.py in read_sas(filepath_or_buffer, format, index, encoding, chunksize, iterator)
     38         filepath_or_buffer = _stringify_path(filepath_or_buffer)
     39         if not isinstance(filepath_or_buffer, compat.string_types):
---> 40             raise ValueError(buffer_error_msg)
     41         try:
     42             fname = filepath_or_buffer.lower()

ValueError: If this is a buffer object rather than a string name, you must specify a format string

Upvotes: 1

Views: 1881

Answers (1)

drum
drum

Reputation: 5651

You are missing the parameter format

import zipfile

zipfile = zipfile.ZipFile('myfile.zip', 'r')
sasfile = zipfile.open('mysasfile.sas7bdat')

df = pd.read_sas(sasfile, format='sas7bdat')

Upvotes: 1

Related Questions