Skip to content

read_sas catches own error #24548

Closed
@samgd

Description

@samgd

Code Sample, a copy-pastable example if possible

import pandas as pd
pd.read_sas('/tmp/foo')

Output (/tmp/foo does not have to exist):

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-10-de3f5c15fb71> in <module>
      1 import pandas as pd
----> 2 pd.read_sas('/tmp/foo')

~/.virtualenvs/pandas/lib/python3.7/site-packages/pandas/io/sas/sasreader.py in read_sas(filepath_or_buffer, format, index, encoding, chunksize, iterator)
     50             pass
     51 
---> 52     if format.lower() == 'xport':
     53         from pandas.io.sas.sas_xport import XportReader
     54         reader = XportReader(filepath_or_buffer, index=index,

AttributeError: 'NoneType' object has no attribute 'lower'

Problem description

When format=None the function attempts to infer the file's format by naively looking at the extension. If it is neither '.xpt' nor '.sas7bdat' then an error is raised. However, this logic is wrapped in a try/except block that catches the raised error and silently discards it:

if format is None:
buffer_error_msg = ("If this is a buffer object rather "
"than a string name, you must specify "
"a format string")
filepath_or_buffer = _stringify_path(filepath_or_buffer)
if not isinstance(filepath_or_buffer, compat.string_types):
raise ValueError(buffer_error_msg)
try:
fname = filepath_or_buffer.lower()
if fname.endswith(".xpt"):
format = "xport"
elif fname.endswith(".sas7bdat"):
format = "sas7bdat"
else:
raise ValueError("unable to infer format of SAS file")
except ValueError:
pass
if format.lower() == 'xport':

The function then attempts to call format.lower() but format is still None so an AttributeError is raised.

Expected Output

Expected behaviour: error raised when unable to infer format. Suggest documenting that inference is done by checking the file's extension.

ValueError: unable to infer format of SAS file

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]
INSTALLED VERSIONS

commit: None
python: 3.7.1.final.0
python-bits: 64
OS: Linux
OS-release: 4.19.12-arch1-1-ARCH
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: en_GB.UTF-8

pandas: 0.23.4
pytest: None
pip: 18.1
setuptools: 40.6.3
Cython: None
numpy: 1.15.4
scipy: None
pyarrow: None
xarray: None
IPython: 7.2.0
sphinx: None
patsy: None
dateutil: 2.7.5
pytz: 2018.7
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions