Skip to content

Commit e1172a3

Browse files
committed
Preserve index
1 parent 41bb87c commit e1172a3

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

pandas/core/frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2234,7 +2234,7 @@ def maybe_reorder(
22342234

22352235
if is_iterator(data):
22362236
if nrows == 0:
2237-
return cls(columns=columns)
2237+
return cls(index=index, columns=columns)
22382238

22392239
try:
22402240
first_row = next(data)

pandas/tests/frame/constructors/test_from_records.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,8 @@ def test_from_records_structured_array(self):
496496
def test_from_records_empty_iterator_with_preserve_columns(self):
497497
# GH#61140
498498
rows = []
499-
result = DataFrame.from_records(iter(rows), columns=["col_1", "Col_2"], nrows=0)
500-
expected = DataFrame([], columns=["col_1", "Col_2"])
499+
result = DataFrame.from_records(
500+
iter(rows), index=[0, 1], columns=["col_1", "Col_2"], nrows=0
501+
)
502+
expected = DataFrame([], index=[0, 1], columns=["col_1", "Col_2"])
501503
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)