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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 7 additions & 1 deletion pandas/io/sas/sasreader.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""
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):

"""
Read SAS files stored as either XPORT or SAS7BDAT format files.

Expand All @@ -29,8 +31,12 @@ 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")
if not isinstance(filepath_or_buffer, compat.string_types):
raise TypeError(buffer_error_msg)
try:
fname = filepath_or_buffer.lower()
if fname.endswith(".xpt"):
Expand Down
11 changes: 11 additions & 0 deletions pandas/io/tests/sas/test_sas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pandas.util.testing as tm
from pandas.compat import StringIO
from pandas import read_sas


class TestSas(tm.TestCase):

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