Skip to content
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

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2231,7 +2231,7 @@ def maybe_reorder(

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

try:
first_row = next(data)
Expand Down
7 changes: 7 additions & 0 deletions pandas/core/frame_test_constructors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import pandas as pd
def test_empty_df_preserve_col():
rows = []
df = pd.DataFrame.from_records(iter(rows), columns=['col_1', 'Col_2'], nrows=0)
assert list(df.columns)==['col_1', 'Col_2']
assert len(df) == 0
Copy link
Member

Choose a reason for hiding this comment

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

Can you follow the dev docs here: https://pandas.pydata.org/pandas-docs/dev/development/contributing_codebase.html#writing-tests

Namely, search the current tests for from_records and that should give a good indication of where to place this test.

Copy link
Member

Choose a reason for hiding this comment

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

This file needs to be removed.


6 changes: 6 additions & 0 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2780,6 +2780,12 @@ def test_construction_nan_value_timedelta64_dtype(self):
)
tm.assert_frame_equal(result, expected)

def test_from_records_empty_iterator_with_preserve_columns(self):
Copy link
Member

Choose a reason for hiding this comment

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

Can you move this test to tests/frame/constructors/test_from_records.py


Copy link
Member

Choose a reason for hiding this comment

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

Can you start the test with a comment referencing the issue. # GH#61140

rows = []
df = pd.DataFrame.from_records(iter(rows), columns=["col_1", "Col_2"], nrows=0)
Copy link
Member

Choose a reason for hiding this comment

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

Can you call the result result instead of df.

assert list(df.columns) == ["col_1", "Col_2"]
assert len(df) == 0
Copy link
Member

Choose a reason for hiding this comment

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

Instead of these two lines, can you check the entire result.

expected = DataFrame(...)
tm.assert_frame_equal(result, expected)`


class TestDataFrameConstructorIndexInference:
def test_frame_from_dict_of_series_overlapping_monthly_period_indexes(self):
Expand Down
Loading