Skip to content

Clarified error in read_sas method when buffer object provided withou… #14947

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

Closed
wants to merge 9 commits into from
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ Performance Improvements
- Improved performance of ``pd.wide_to_long()`` (:issue:`14779`)
- Increased performance of ``pd.factorize()`` by releasing the GIL with ``object`` dtype when inferred as strings (:issue:`14859`)

- When reading buffer object in ``read_sas()`` method without specified format, filepath string is inferred rather than buffer object.
Copy link
Contributor

@jreback jreback Dec 31, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug when reading a buffer object in pd.read_sas(), without a specified format, a filepath string was inferred rather than buffer object.

move to Bug Fix section, add the issue number (this PR) at the end



.. _whatsnew_0200.bug_fixes:
Expand Down
7 changes: 5 additions & 2 deletions pandas/io/sas/sasreader.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Read SAS sas7bdat or xport files.
"""

from pandas import compat

def read_sas(filepath_or_buffer, format=None, index=None, encoding=None,
chunksize=None, iterator=False):
Expand Down Expand Up @@ -29,8 +29,11 @@ def read_sas(filepath_or_buffer, format=None, index=None, encoding=None,
DataFrame if iterator=False and chunksize=None, else SAS7BDATReader
or XportReader
"""

if format is None:
buffer_error_msg = "If this is a buffer object rather\
than a string name, you must specify a format string"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use

("..."
 "...")

instead of \ for line continuation?

if not isinstance(filepath_or_buffer,compat.string_types):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have some PEP8 errors (we check for them in the travis build, that is the reason travis failed). For example, here there should be a space after the comma.

Here is the full list (you can find it at the bottom of the failing test (third travis build), but I also recommend setting up your IDE to check for this):

pandas/io/sas/sasreader.py:6:1: E302 expected 2 blank lines, found 1
pandas/io/sas/sasreader.py:35:45: E231 missing whitespace after ','
pandas/io/tests/sas/test_sas.py:5:1: E302 expected 2 blank lines, found 1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I got this covered. I'll reread the Pandas submission info for how to ensure PEP8 compliance tonight.

raise TypeError(buffer_error_msg)
try:
fname = filepath_or_buffer.lower()
if fname.endswith(".xpt"):
Expand Down
9 changes: 9 additions & 0 deletions pandas/io/tests/sas/test_sas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pandas.util.testing as tm
from pandas.compat import StringIO
from pandas import read_sas

class TestSasBuff(tm.TestCase):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can call this just TestSas (and there needs to be a blank line after this one)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

def test_sas_buffer_format(self):
b = StringIO("")
with self.assertRaises(TypeError):
read_sas(b)