Skip to content

Commit da25eed

Browse files
Fix read_csv IndexError crash for c engine with header=None and 2 (or more) extra columns
1 parent 5b18d3c commit da25eed

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

pandas/_libs/parsers.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1317,8 +1317,8 @@ cdef class TextReader:
13171317
else:
13181318
if self.header is not None:
13191319
j = i - self.leading_cols
1320-
# hack for #2442
1321-
if j == len(self.header[0]):
1320+
# hack for #2442 and #26218
1321+
if j >= len(self.header[0]):
13221322
return j
13231323
else:
13241324
return self.header[0][j]

pandas/tests/io/parser/test_common.py

+10
Original file line numberDiff line numberDiff line change
@@ -2116,3 +2116,13 @@ def test_blank_lines_between_header_and_data_rows(all_parsers, nrows):
21162116
parser = all_parsers
21172117
df = parser.read_csv(StringIO(csv), header=3, nrows=nrows, skip_blank_lines=False)
21182118
tm.assert_frame_equal(df, ref[:nrows])
2119+
2120+
2121+
def test_no_header_two_extra_columns(all_parsers):
2122+
# GH 26218
2123+
column_names = ["one", "two", "three"]
2124+
ref = DataFrame([["foo", "bar", "baz"]], columns=column_names)
2125+
stream = StringIO("foo,bar,baz,bam,blah")
2126+
parser = all_parsers
2127+
df = parser.read_csv(stream, header=None, names=column_names, index_col=False)
2128+
tm.assert_frame_equal(df, ref)

0 commit comments

Comments
 (0)