Skip to content

Commit f0529a7

Browse files
committed
Fix reading SAS7BDAT files with zero rows
1 parent 3e3bb90 commit f0529a7

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

doc/source/whatsnew/v1.5.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,7 @@ I/O
813813
- Bug in :func:`read_excel` when reading a ``.ods`` file with newlines between xml elements (:issue:`45598`)
814814
- Bug in :func:`read_parquet` when ``engine="fastparquet"`` where the file was not closed on error (:issue:`46555`)
815815
- :meth:`to_html` now excludes the ``border`` attribute from ``<table>`` elements when ``border`` keyword is set to ``False``.
816+
- Bug in :func:`read_sas` returned ``None`` rather than an empty DataFrame for SAS7BDAT files with zero rows (:issue:`18198`)
816817
-
817818

818819
Period

pandas/io/sas/sas7bdat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ def read(self, nrows: int | None = None) -> DataFrame | None:
737737
self.close()
738738
raise EmptyDataError("No columns to parse from file")
739739

740-
if self._current_row_in_file_index >= self.row_count:
740+
if nrows > 0 and self._current_row_in_file_index >= self.row_count:
741741
return None
742742

743743
m = self.row_count - self._current_row_in_file_index

pandas/tests/io/sas/test_sas7bdat.py

+9
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,15 @@ def test_zero_variables(datapath):
216216
pd.read_sas(fname)
217217

218218

219+
def test_zero_rows(datapath):
220+
# GH 18198
221+
fname = datapath("io", "sas", "data", "zero_rows.sas7bdat")
222+
df = pd.read_sas(fname)
223+
tm.assert_frame_equal(
224+
df, pd.DataFrame([{"char_field": "a", "num_field": 1.0}]).iloc[:0]
225+
)
226+
227+
219228
def test_corrupt_read(datapath):
220229
# We don't really care about the exact failure, the important thing is
221230
# that the resource should be cleaned up afterwards (BUG #35566)

0 commit comments

Comments
 (0)