Skip to content

Commit 1e93c75

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 1e93c75

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

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)