diff --git a/doc/source/whatsnew/v0.20.0.txt b/doc/source/whatsnew/v0.20.0.txt index 27f8564ba16c2..b70b3c1cea2b5 100644 --- a/doc/source/whatsnew/v0.20.0.txt +++ b/doc/source/whatsnew/v0.20.0.txt @@ -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. .. _whatsnew_0200.bug_fixes: diff --git a/pandas/io/sas/sasreader.py b/pandas/io/sas/sasreader.py index 081d780f71cb3..6f7e4c0d213bc 100644 --- a/pandas/io/sas/sasreader.py +++ b/pandas/io/sas/sasreader.py @@ -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. @@ -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"): diff --git a/pandas/io/tests/sas/test_sas.py b/pandas/io/tests/sas/test_sas.py new file mode 100644 index 0000000000000..2a55a2d68157c --- /dev/null +++ b/pandas/io/tests/sas/test_sas.py @@ -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)