Skip to content

BUG: Throw a ParserError when header rows have unequal column counts … #43118

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

Merged
merged 22 commits into from
Sep 5, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
53a1768
BUG: Throw a ParserError when header rows have unequal column counts …
quantumalaviya Aug 20, 2021
128b4e3
BUG: Throw a ParserError when header rows have unequal column counts.…
quantumalaviya Aug 20, 2021
6483df7
Merge remote-tracking branch 'upstream/master' into b43102
quantumalaviya Aug 20, 2021
bec4d00
Merge remote-tracking branch 'upstream/master' into b43102
quantumalaviya Aug 20, 2021
95bac98
Added Test. (GH43102)
quantumalaviya Aug 20, 2021
10422a8
Added Test. (GH43102)
quantumalaviya Aug 20, 2021
658c291
Added Test. (GH43102)
quantumalaviya Aug 20, 2021
a02d476
Added Changes. (GH43102)
quantumalaviya Aug 20, 2021
87bf9a2
Merge remote-tracking branch 'upstream/master' into b43102
quantumalaviya Aug 21, 2021
f1b1a89
Merge remote-tracking branch 'upstream/master' into b43102
quantumalaviya Aug 21, 2021
fb963d8
Merge remote-tracking branch 'upstream/master' into b43102
quantumalaviya Aug 22, 2021
5f534ea
Added whatsnew
quantumalaviya Aug 22, 2021
5239ece
Added whatsnew
quantumalaviya Aug 22, 2021
0dbc0cc
Merge remote-tracking branch 'upstream/master' into b43102
quantumalaviya Aug 23, 2021
863e996
Test without whatsnew
quantumalaviya Aug 23, 2021
532e6cb
Add whatsnew again
quantumalaviya Aug 23, 2021
2533bd1
Merge branch 'master' into b43102
quantumalaviya Sep 4, 2021
7d01f0a
Merged upstream
quantumalaviya Sep 5, 2021
1caf42d
Update v1.4.0.rst
quantumalaviya Sep 5, 2021
921b57d
Merged upstream
quantumalaviya Sep 5, 2021
2ca6ccf
Merge upstream
quantumalaviya Sep 5, 2021
3f1fb39
Skipping test on PyArrow
quantumalaviya Sep 5, 2021
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
8 changes: 8 additions & 0 deletions pandas/io/parsers/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,14 @@ def _extract_multi_indexer_columns(
# extract the columns
field_count = len(header[0])

# check if header lengths are equal
for header_iter in range(len(header)):
Copy link
Member

@phofl phofl Aug 20, 2021

Choose a reason for hiding this comment

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

You could do this like all(len(x) == len(ls[0]) for x in ls[1:])

has the downside that the element where the error was found is not clear

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So you do suggest removing that part of the error? It can simply say "Header rows must have an equal number of columns."

Copy link
Member

Choose a reason for hiding this comment

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

Yeah I would go with that.

if len(header[header_iter]) != field_count:
raise ParserError(
"Header rows must have equal number of columns. "
f"Mismatch found at header {header_iter}."
)

def extract(r):
return tuple(r[i] for i in range(field_count) if i not in sic)

Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/io/parser/test_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,3 +585,20 @@ def test_read_csv_multiindex_columns(all_parsers):
tm.assert_frame_equal(df1, expected.iloc[:1])
df2 = parser.read_csv(StringIO(s2), header=[0, 1])
tm.assert_frame_equal(df2, expected)


def test_read_csv_multi_header_length_check(all_parsers):
# GH#43102
parser = all_parsers

case = """row11,row12,row13
row21,row22, row23
row31,row32
"""

with pytest.raises(
ParserError,
match="Header rows must have equal number of columns. "
"Mismatch found at header 1.",
):
parser.read_csv(StringIO(case), sep=",", header=[0, 2])
Copy link
Member

Choose a reason for hiding this comment

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

sep is the default

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, I will get rid of it, thanks