Skip to content

BUG: Preserve column names in DataFrame.from_records when nrows=0 #62085

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 11 commits into from
Aug 11, 2025
Merged
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,7 @@ I/O
- Bug in :meth:`.DataFrame.to_json` when ``"index"`` was a value in the :attr:`DataFrame.column` and :attr:`Index.name` was ``None``. Now, this will fail with a ``ValueError`` (:issue:`58925`)
- Bug in :meth:`.io.common.is_fsspec_url` not recognizing chained fsspec URLs (:issue:`48978`)
- Bug in :meth:`DataFrame._repr_html_` which ignored the ``"display.float_format"`` option (:issue:`59876`)
- Bug in :meth:`DataFrame.from_records` ignoring ``columns`` parameter when ``data`` is an iterator and ``nrows=0``. (:issue:`61140`)
- Bug in :meth:`DataFrame.from_records` where ``columns`` parameter with numpy structured array was not reordering and filtering out the columns (:issue:`59717`)
- Bug in :meth:`DataFrame.to_dict` raises unnecessary ``UserWarning`` when columns are not unique and ``orient='tight'``. (:issue:`58281`)
- Bug in :meth:`DataFrame.to_excel` when writing empty :class:`DataFrame` with :class:`MultiIndex` on both axes (:issue:`57696`)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2234,7 +2234,7 @@ def maybe_reorder(

if is_iterator(data):
if nrows == 0:
return cls()
return cls(columns=columns)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does index need to be preserved here as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, to be consistent with empty iterator and nrows > 0. I updated the PR.


try:
first_row = next(data)
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/frame/constructors/test_from_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,3 +492,10 @@ def test_from_records_structured_array(self):
expected_result = DataFrame(modified_data)

tm.assert_frame_equal(actual_result, expected_result)

def test_from_records_empty_iterator_with_preserve_columns(self):
# GH#61140
rows = []
result = DataFrame.from_records(iter(rows), columns=["col_1", "Col_2"], nrows=0)
expected = DataFrame([], columns=["col_1", "Col_2"])
tm.assert_frame_equal(result, expected)
Loading