BUG: Preserve column names in DataFrame.from_records when nrows=0#61143
BUG: Preserve column names in DataFrame.from_records when nrows=0#61143kpvenkat47 wants to merge 5 commits intopandas-dev:mainfrom
Conversation
…if nrows == 0' to return Cls(columns=columns) in core/frame.py. - Added test to verify column preservation.
There was a problem hiding this comment.
Thanks for the PR! When making a PR, follow these steps here:
https://pandas.pydata.org/pandas-docs/dev/development/contributing.html#making-a-pull-request
namely step 4.
| 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
This file needs to be removed.
|
I have updated the PR with the latest changes based on feedback. Please review again. |
|
Looks good! The change in One minor point: It looks like the test was also added as a new standalone file |
rhshadrach
left a comment
There was a problem hiding this comment.
Looking good! Will also need to add to the whatsnew for v3.0.0 in the I/O section.
| tm.assert_frame_equal(result, expected) | ||
|
|
||
| def test_from_records_empty_iterator_with_preserve_columns(self): | ||
|
|
There was a problem hiding this comment.
Can you start the test with a comment referencing the issue. # GH#61140
| 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 |
There was a problem hiding this comment.
This file needs to be removed.
| ) | ||
| tm.assert_frame_equal(result, expected) | ||
|
|
||
| def test_from_records_empty_iterator_with_preserve_columns(self): |
There was a problem hiding this comment.
Can you move this test to tests/frame/constructors/test_from_records.py
| def test_from_records_empty_iterator_with_preserve_columns(self): | ||
|
|
||
| rows = [] | ||
| df = pd.DataFrame.from_records(iter(rows), columns=["col_1", "Col_2"], nrows=0) |
There was a problem hiding this comment.
Can you call the result result instead of df.
| assert list(df.columns) == ["col_1", "Col_2"] | ||
| assert len(df) == 0 |
There was a problem hiding this comment.
Instead of these two lines, can you check the entire result.
expected = DataFrame(...)
tm.assert_frame_equal(result, expected)`|
I have updated the changes. Please check and let me know if any improvements are needed. Thank you |
|
@kpvenkat47 - as mentioned we should add a line to the whatsnew for v3.0 in |
|
This pull request is stale because it has been open for thirty days with no activity. Please update and respond to this comment if you're still interested in working on this. |
|
Thanks for the pull request, but it appears to have gone stale. If interested in continuing, please merge in the main branch, address any review comments and/or failing tests, and we can reopen. |
Description
Updates pandas/core/frame.py to preserve column names in empty DataFrames when nrows == 0. Changed from return Cls() to return Cls(columns=columns).
Closes #61140
Changes Made
Testing