Skip to content

Commit cc21929

Browse files
committed
Update empty DataFrame initialization to preserve columns - Changed 'if nrows == 0' to return Cls(columns=columns) in core/frame.py. - Added test to verify column preservation.
1 parent 882fa9c commit cc21929

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

pandas/core/frame.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2231,7 +2231,7 @@ def maybe_reorder(
22312231

22322232
if is_iterator(data):
22332233
if nrows == 0:
2234-
return cls()
2234+
return cls(columns=columns)
22352235

22362236
try:
22372237
first_row = next(data)
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import pandas as pd
2+
def test_empty_df_preserve_col():
3+
rows = []
4+
df = pd.DataFrame.from_records(iter(rows), columns=['col_1', 'Col_2'], nrows=0)
5+
assert list(df.columns)==['col_1', 'Col_2']
6+
assert len(df) == 0
7+

0 commit comments

Comments
 (0)