-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: read_csv raising IndexError with multiple header cols, specified index_col and no data rows #38325
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: read_csv raising IndexError with multiple header cols, specified index_col and no data rows #38325
Changes from 2 commits
3a21002
415c246
580df13
6371423
27dbe00
0836a08
e49a6d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1465,9 +1465,14 @@ def _extract_multi_indexer_columns( | |
|
||
# clean the index_names | ||
index_names = header.pop(-1) | ||
index_names, names, index_col = _clean_index_names( | ||
index_names, self.index_col, self.unnamed_cols | ||
) | ||
if not index_names: | ||
# In case of no rows and multiindex columns we have to set index_names to | ||
# list of Nones GH#38292 | ||
index_names = [None] * len(ic) | ||
else: | ||
index_names, _, _ = _clean_index_names( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you generate this in _clean_index_names instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
index_names, self.index_col, self.unnamed_cols | ||
) | ||
|
||
# extract the columns | ||
field_count = len(header[0]) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -222,3 +222,18 @@ def test_index_col_large_csv(all_parsers): | |
result = parser.read_csv(path, index_col=[0]) | ||
|
||
tm.assert_frame_equal(result, df.set_index("a")) | ||
|
||
|
||
def test_index_col_multiindex_columns_no_data(all_parsers): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add all the cases form the op (3 working & the non-working) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
# GH#38292 | ||
parser = all_parsers | ||
result = parser.read_csv( | ||
StringIO("a0,a1,a2\nb0,b1,b2\n"), header=[0, 1], index_col=0 | ||
) | ||
expected = DataFrame( | ||
[], | ||
columns=MultiIndex.from_arrays( | ||
[["a1", "a2"], ["b1", "b2"]], names=["a0", "b0"] | ||
), | ||
) | ||
tm.assert_frame_equal(result, expected) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will need to move to 1.3 (avail shortly)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you remove from 1.2 :->
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thx, done