Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read_sas catches own error #24548

Closed
samgd opened this issue Jan 2, 2019 · 4 comments · Fixed by #24554
Closed

read_sas catches own error #24548

samgd opened this issue Jan 2, 2019 · 4 comments · Fixed by #24554
Labels
Bug IO SAS SAS: read_sas
Milestone

Comments

@samgd
Copy link

samgd commented Jan 2, 2019

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

@benjaminr
Copy link
Contributor

I see what you mean. Could quickly be resolved by swapping pass to raise and Updating the docstring. I'll add a PR for these changes.

@samgd
Copy link
Author

samgd commented Jan 2, 2019

Is the try/except clause even required?filepath_or_buffer is a string given the above guard statement so both lower() and endswith should be error free?

@samgd
Copy link
Author

samgd commented Jan 2, 2019

If the try statement is there to catch something in the logic I've missed then changing it to:

try:
     ...
except ValueError:
    raise

is odd as this is essentially a noop?

@benjaminr
Copy link
Contributor

@samgd yeah you're right. Will update to reflect that.

@jreback jreback added this to the 0.24.0 milestone Jan 2, 2019
jreback pushed a commit that referenced this issue Jan 3, 2019
Pingviinituutti pushed a commit to Pingviinituutti/pandas that referenced this issue Feb 28, 2019
Pingviinituutti pushed a commit to Pingviinituutti/pandas that referenced this issue Feb 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO SAS SAS: read_sas
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants