Skip to content

Commit e7eefc4

Browse files
tomrodjreback
authored andcommitted
ERR: Clarified error in read_sas method when buffer object provided without a format
Author: tomrod <[email protected]> Closes pandas-dev#14947 from tomrod/sas_read_format_bugfix and squashes the following commits: 1285dbb [tomrod] flake8 testing 4cf9231 [tomrod] PEP8 compliance ab76d80 [tomrod] Updating to match pep8 whitespace requirements in sasreader.py ffdce1d [tomrod] Updating to match pep8 as per @jorisvandenbossche aa1ada3 [tomrod] More specific error message, moved imports to top of files bf60d23 [tomrod] Adding tests, creating updated information 5efdb85 [tomrod] Adding tests f8166fc [tomrod] Updated based on feedback from jreback b52f204 [tomrod] Clarified error in read_sas method when buffer object provided without format
1 parent 8bf6852 commit e7eefc4

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

doc/source/whatsnew/v0.20.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ Performance Improvements
282282
- Improved performance of ``pd.wide_to_long()`` (:issue:`14779`)
283283
- Increased performance of ``pd.factorize()`` by releasing the GIL with ``object`` dtype when inferred as strings (:issue:`14859`)
284284

285+
- When reading buffer object in ``read_sas()`` method without specified format, filepath string is inferred rather than buffer object.
285286

286287

287288
.. _whatsnew_0200.bug_fixes:

pandas/io/sas/sasreader.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
"""
22
Read SAS sas7bdat or xport files.
33
"""
4+
from pandas import compat
45

56

67
def read_sas(filepath_or_buffer, format=None, index=None, encoding=None,
78
chunksize=None, iterator=False):
9+
810
"""
911
Read SAS files stored as either XPORT or SAS7BDAT format files.
1012
@@ -29,8 +31,12 @@ def read_sas(filepath_or_buffer, format=None, index=None, encoding=None,
2931
DataFrame if iterator=False and chunksize=None, else SAS7BDATReader
3032
or XportReader
3133
"""
32-
3334
if format is None:
35+
buffer_error_msg = ("If this is a buffer object rather "
36+
"than a string name, you must specify "
37+
"a format string")
38+
if not isinstance(filepath_or_buffer, compat.string_types):
39+
raise ValueError(buffer_error_msg)
3440
try:
3541
fname = filepath_or_buffer.lower()
3642
if fname.endswith(".xpt"):

pandas/io/tests/sas/test_sas.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import pandas.util.testing as tm
2+
from pandas.compat import StringIO
3+
from pandas import read_sas
4+
5+
6+
class TestSas(tm.TestCase):
7+
8+
def test_sas_buffer_format(self):
9+
10+
# GH14947
11+
b = StringIO("")
12+
with self.assertRaises(ValueError):
13+
read_sas(b)

0 commit comments

Comments
 (0)