Skip to content

BUG: IndexError when header rows have unequal column counts #43102

Closed
@MrCreosote

Description

@MrCreosote
  • I have checked that this issue has not already been reported.

I couldn't find it with a search on indexerror read_csv anyway

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

In [1]: import pandas

In [2]: from io import StringIO

In [3]: data = """
   ...: head1, head2, head3
   ...: sub1, sub2
   ...: """[1:]

In [4]: data
Out[4]: 'head1, head2, head3\nsub1, sub2\n'

In [5]: pandas.read_csv(StringIO(data), sep=",", header=[0, 1], on_bad_lines="sk
   ...: ip")
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-5-1bf8982a1449> in <module>
----> 1 pandas.read_csv(StringIO(data), sep=",", header=[0, 1], on_bad_lines="skip")

*snip - I can provide the entire trace on request, but leave it out here for brevity*

~/.local/share/virtualenvs/py3.9playground--W4ZB8cD/lib/python3.9/site-packages/pandas/io/parsers/base_parser.py in <genexpr>(.0)
    343 
    344         def extract(r):
--> 345             return tuple(r[i] for i in range(field_count) if i not in sic)
    346 
    347         columns = list(zip(*(extract(r) for r in header)))

IndexError: list index out of range

Problem description

A generic index error is thrown rather than a pandas specific error, or skipping the row as per on_bad_lines.

Expected Output

I would expect one of two things:

  1. a pandas.errors.ParseError describing the problem
  2. skipping the header. This seems a little odd to me so maybe 1 is the better option.

Maybe there are other reasonable responses as well.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 5f648bf
python : 3.9.6.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-154-generic
Version : #161-Ubuntu SMP Fri Jul 30 13:04:17 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.3.2
numpy : 1.21.2
pytz : 2021.1
dateutil : 2.8.2
pip : 20.3.3
setuptools : 51.3.3
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 7.26.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : 3.0.7
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 2.0.1
xlwt : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugError ReportingIncorrect or improved errors from pandasIO CSVread_csv, to_csv

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions