Skip to content
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 @@ -837,6 +837,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`` and ``index`` parameters when ``data`` is an empty 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(index=index, columns=columns)

try:
first_row = next(data)
Expand Down
9 changes: 9 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,12 @@ 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), index=[0, 1], columns=["col_1", "Col_2"], nrows=0
)
expected = DataFrame([], index=[0, 1], columns=["col_1", "Col_2"])
tm.assert_frame_equal(result, expected)
Loading