Skip to content

Commit d47eae8

Browse files
authored
Fix reading SAS7BDAT files with zero rows (#47116)
* Fix reading SAS7BDAT files with zero rows * Add missing file * Update test_sas7bdat.py
1 parent b913935 commit d47eae8

File tree

4 files changed

+10
-1
lines changed

4 files changed

+10
-1
lines changed

doc/source/whatsnew/v1.5.0.rst

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

824825
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
256 KB
Binary file not shown.

pandas/tests/io/sas/test_sas7bdat.py

+8
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,14 @@ 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+
result = pd.read_sas(fname)
223+
expected = pd.DataFrame([{"char_field": "a", "num_field": 1.0}]).iloc[:0]
224+
tm.assert_frame_equal(result, expected)
225+
226+
219227
def test_corrupt_read(datapath):
220228
# We don't really care about the exact failure, the important thing is
221229
# that the resource should be cleaned up afterwards (BUG #35566)

0 commit comments

Comments
 (0)