Skip to content

Commit c08de6b

Browse files
benjaminrjreback
authored andcommitted
read_sas catches own error #24548 (#24554)
1 parent c217656 commit c08de6b

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1599,6 +1599,7 @@ Notice how we now instead output ``np.nan`` itself instead of a stringified form
15991599
- :func:`read_sas()` will parse numbers in sas7bdat-files that have width less than 8 bytes correctly. (:issue:`21616`)
16001600
- :func:`read_sas()` will correctly parse sas7bdat files with many columns (:issue:`22628`)
16011601
- :func:`read_sas()` will correctly parse sas7bdat files with data page types having also bit 7 set (so page type is 128 + 256 = 384) (:issue:`16615`)
1602+
- Bug in :func:`read_sas()` in which an incorrect error was raised on an invalid file format. (:issue:`24548`)
16021603
- Bug in :meth:`detect_client_encoding` where potential ``IOError`` goes unhandled when importing in a mod_wsgi process due to restricted access to stdout. (:issue:`21552`)
16031604
- Bug in :func:`to_html()` with ``index=False`` misses truncation indicators (...) on truncated DataFrame (:issue:`15019`, :issue:`22783`)
16041605
- Bug in :func:`to_html()` with ``index=False`` when both columns and row index are ``MultiIndex`` (:issue:`22579`)

pandas/io/sas/sasreader.py

+9-12
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def read_sas(filepath_or_buffer, format=None, index=None, encoding=None,
1616
filepath_or_buffer : string or file-like object
1717
Path to the SAS file.
1818
format : string {'xport', 'sas7bdat'} or None
19-
If None, file format is inferred. If 'xport' or 'sas7bdat',
20-
uses the corresponding format.
19+
If None, file format is inferred from file extension. If 'xport' or
20+
'sas7bdat', uses the corresponding format.
2121
index : identifier of index column, defaults to None
2222
Identifier of column that should be used as index of the DataFrame.
2323
encoding : string, default is None
@@ -39,16 +39,13 @@ def read_sas(filepath_or_buffer, format=None, index=None, encoding=None,
3939
filepath_or_buffer = _stringify_path(filepath_or_buffer)
4040
if not isinstance(filepath_or_buffer, compat.string_types):
4141
raise ValueError(buffer_error_msg)
42-
try:
43-
fname = filepath_or_buffer.lower()
44-
if fname.endswith(".xpt"):
45-
format = "xport"
46-
elif fname.endswith(".sas7bdat"):
47-
format = "sas7bdat"
48-
else:
49-
raise ValueError("unable to infer format of SAS file")
50-
except ValueError:
51-
pass
42+
fname = filepath_or_buffer.lower()
43+
if fname.endswith(".xpt"):
44+
format = "xport"
45+
elif fname.endswith(".sas7bdat"):
46+
format = "sas7bdat"
47+
else:
48+
raise ValueError("unable to infer format of SAS file")
5249

5350
if format.lower() == 'xport':
5451
from pandas.io.sas.sas_xport import XportReader

pandas/tests/io/sas/test_sas.py

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from pandas.compat import StringIO
44

55
from pandas import read_sas
6+
import pandas.util.testing as tm
67

78

89
class TestSas(object):
@@ -15,3 +16,10 @@ def test_sas_buffer_format(self):
1516
"name, you must specify a format string")
1617
with pytest.raises(ValueError, match=msg):
1718
read_sas(b)
19+
20+
def test_sas_read_no_format_or_extension(self):
21+
# see gh-24548
22+
msg = ("unable to infer format of SAS file")
23+
with tm.ensure_clean('test_file_no_extension') as path:
24+
with pytest.raises(ValueError, match=msg):
25+
read_sas(path)

0 commit comments

Comments
 (0)