Skip to content

Fix reading SAS7BDAT files with zero rows #47116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,7 @@ I/O
- Bug in :func:`read_excel` when reading a ``.ods`` file with newlines between xml elements (:issue:`45598`)
- Bug in :func:`read_parquet` when ``engine="fastparquet"`` where the file was not closed on error (:issue:`46555`)
- :meth:`to_html` now excludes the ``border`` attribute from ``<table>`` elements when ``border`` keyword is set to ``False``.
- Bug in :func:`read_sas` returned ``None`` rather than an empty DataFrame for SAS7BDAT files with zero rows (:issue:`18198`)
-

Period
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/sas/sas7bdat.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ def read(self, nrows: int | None = None) -> DataFrame | None:
self.close()
raise EmptyDataError("No columns to parse from file")

if self._current_row_in_file_index >= self.row_count:
if nrows > 0 and self._current_row_in_file_index >= self.row_count:
return None

m = self.row_count - self._current_row_in_file_index
Expand Down
Binary file added pandas/tests/io/sas/data/zero_rows.sas7bdat
Binary file not shown.
9 changes: 9 additions & 0 deletions pandas/tests/io/sas/test_sas7bdat.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,15 @@ def test_zero_variables(datapath):
pd.read_sas(fname)


def test_zero_rows(datapath):
# GH 18198
fname = datapath("io", "sas", "data", "zero_rows.sas7bdat")
df = pd.read_sas(fname)
tm.assert_frame_equal(
df, pd.DataFrame([{"char_field": "a", "num_field": 1.0}]).iloc[:0]
)


def test_corrupt_read(datapath):
# We don't really care about the exact failure, the important thing is
# that the resource should be cleaned up afterwards (BUG #35566)
Expand Down