Skip to content

Commit 90d100c

Browse files
committed
sas7bdat: Check if the SAS file has zero variables
If the given SAS file has 0 rows, throw an error for the EmptyData file. When reading, check that the column information is available. If not, throw an error.
1 parent 8dac633 commit 90d100c

File tree

4 files changed

+15
-0
lines changed

4 files changed

+15
-0
lines changed

doc/source/whatsnew/v0.22.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ Bug Fixes
9090
- Bug in ``pd.read_msgpack()`` with a non existent file is passed in Python 2 (:issue:`15296`)
9191
- Bug in ``DataFrame.groupby`` where key as tuple in a ``MultiIndex`` were interpreted as a list of keys (:issue:`17979`)
9292
- Bug in :func:`pd.read_csv` where a ``MultiIndex`` with duplicate columns was not being mangled appropriately (:issue:`18062`)
93+
- Bug in :func:`pd.read_sas` where a file with 0 variables gave an ``AttributeError`` incorrectly. Now it gives an ``EmptyDataError``
9394

9495
Conversion
9596
^^^^^^^^^^

pandas/io/sas/sas7bdat.py

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import pandas as pd
1818
from pandas import compat
1919
from pandas.io.common import get_filepath_or_buffer, BaseIterator
20+
from pandas.errors import EmptyDataError
2021
import numpy as np
2122
import struct
2223
import pandas.io.sas.sas_constants as const
@@ -594,6 +595,9 @@ def read(self, nrows=None):
594595
elif nrows is None:
595596
nrows = self.row_count
596597

598+
if len(self.column_types) == 0:
599+
raise EmptyDataError("No columns to parse from file")
600+
597601
if self._current_row_in_file_index >= self.row_count:
598602
return None
599603

Binary file not shown.

pandas/tests/io/sas/test_sas7bdat.py

+10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import pandas as pd
22
from pandas.compat import PY2
33
import pandas.util.testing as tm
4+
from pandas.errors import EmptyDataError
45
import os
56
import io
67
import numpy as np
8+
import pytest
79

810

911
class TestSAS7BDAT(object):
@@ -174,3 +176,11 @@ def test_date_time():
174176
df0 = pd.read_csv(fname, parse_dates=['Date1', 'Date2', 'DateTime',
175177
'DateTimeHi', 'Taiw'])
176178
tm.assert_frame_equal(df, df0)
179+
180+
181+
def test_zero_variables():
182+
# Check if the SAS file has zero variables (PR #18184)
183+
dirpath = tm.get_data_path()
184+
fname = os.path.join(dirpath, "zero_variables.sas7bdat")
185+
with pytest.raises(EmptyDataError):
186+
pd.read_sas(fname)

0 commit comments

Comments
 (0)