Skip to content

BUG: read_excel fails with IndexError: list index out of range #39250

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

Closed
2 of 3 tasks
YarShev opened this issue Jan 18, 2021 · 11 comments · Fixed by #39482
Closed
2 of 3 tasks

BUG: read_excel fails with IndexError: list index out of range #39250

YarShev opened this issue Jan 18, 2021 · 11 comments · Fixed by #39482
Labels
Bug Error Reporting Incorrect or improved errors from pandas IO Excel read_excel, to_excel Regression Functionality that used to work in a prior pandas version Upstream issue Issue related to pandas dependency
Milestone

Comments

@YarShev
Copy link
Contributor

YarShev commented Jan 18, 2021

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

  • 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

# Your code here
import pandas
df = pandas.read_excel("excel_sheetname_title.xlsx")
IndexError: list index out of range

pandas : 1.2.0
numpy : 1.19.2
pytz : 2020.5
dateutil : 2.8.1
pip : 20.3.3
setuptools : 51.1.2.post20210112
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 : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : 3.0.6
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

The file which read_excel fails on.
excel_sheetname_title.xlsx

@YarShev YarShev added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 18, 2021
@asishm
Copy link
Contributor

asishm commented Jan 18, 2021

full traceback please.

I can't open this file with Excel (It tries to recover then fails)

@YarShev
Copy link
Contributor Author

YarShev commented Jan 18, 2021

df = pandas.read_excel("excel_sheetname_title.xlsx")
Traceback (most recent call last):
File "", line 1, in
File "miniconda3\envs\pandas\lib\site-packages\pandas\util_decorators.py", line 299, in wrapper
return func(*args, **kwargs)
File "miniconda3\envs\pandas\lib\site-packages\pandas\io\excel_base.py", line 344, in read_excel
data = io.parse(
File "miniconda3\envs\pandas\lib\site-packages\pandas\io\excel_base.py", line 1153, in parse
return self._reader.parse(
File "miniconda3\envs\pandas\lib\site-packages\pandas\io\excel_base.py", line 479, in parse
sheet = self.get_sheet_by_index(asheetname)
File "miniconda3\envs\pandas\lib\site-packages\pandas\io\excel_openpyxl.py", line 498, in get_sheet_by_index
return self.book.worksheets[index]
IndexError: list index out of range

@YarShev
Copy link
Contributor Author

YarShev commented Jan 18, 2021

I can't open this file with Excel (It tries to recover then fails)

Yes, I have the same issue. However, my excel recovered the file and I can open it.

@YarShev
Copy link
Contributor Author

YarShev commented Jan 18, 2021

I have figured out why the exception is arisen. The file, which I provided, can be read via xlrd <= 1.2.0 (maybe via even other xlrd versions, but not >= 2.0). However, pandas 1.2.0 uses openpyxl to read the file, if it is installed, that leads to the exception.

@asishm
Copy link
Contributor

asishm commented Jan 18, 2021

yes, this is probably an openpyxl bug then.

In [131]: wb = openpyxl.load_workbook(path, read_only=False) # or read_only=True - which is what pandas actually uses, but it doesn't affect the results for this file

In [132]: wb.sheetnames
Out[132]: []

In [133]: wb.read_only
Out[133]: False

In [134]: wb.worksheets
Out[134]: []

wb.worksheets is an empty list and pandas does sheet = wb.worksheets[0] which is what fails with the IndexError. Maybe a better error message here?

@asishm
Copy link
Contributor

asishm commented Jan 18, 2021

did some digging - again - this is if anything an upstream issue.

openpyxl has this block of code:

# openpyxl/reader/excel.py in ExcelReader.read_worksheets
        for sheet, rel in self.parser.find_sheets():
            if rel.target not in self.valid_files: # this results in True for all the sheets
                continue

the if statement becomes True for all the sheets.
self.valid_files is this list

['_rels/',
 '_rels/.rels',
 '[Content_Types].xml',
 'docProps/',
 'docProps/app.xml',
 'docProps/core.xml',
 'xl/',
 'xl/workbook.xml',
 'xl/.DS_Store',
 '__MACOSX/xl/._.DS_Store',
 'xl/worksheets/',
 'xl/styles.xml',
 'xl/theme/',
 'xl/_rels/',
 'xl/sharedStrings.xml',
 'xl/worksheets/Sheet2.xml',
 'xl/worksheets/Sheet3.xml',
 'xl/worksheets/Sheet1.xml',
 'xl/worksheets/Sheet4.xml',
 'xl/theme/theme1.xml',
 'xl/_rels/workbook.xml.rels']

but the relationships file (rel.target) has the filenames of the sheets as lowercase

['xl/worksheets/sheet1.xml',
 'xl/worksheets/sheet2.xml',
 'xl/worksheets/sheet3.xml',
 'xl/worksheets/sheet4.xml']

This probably indicates this is a malformed excel file (given that Excel also tries to "fix" the errors) - so openpyxl may be doing the right thing. I don't particularly intend on reading up the specs as to whether these should be case sensitive or not.

Re-saving the zip after correctly capitalizing the 's' in 'sheetX.xml' in the rels file (xl/_rels/workbook.xml.rels) makes it readable.

@YarShev
Copy link
Contributor Author

YarShev commented Jan 18, 2021

Yes, more accurate error message would be better.

@simonjayhawkins
Copy link
Member

Thanks @YarShev for the report

since this worked in 1.1.5, i'll label as regression.

Yes, more accurate error message would be better.

further investigations/PRs welcome

@simonjayhawkins simonjayhawkins added Error Reporting Incorrect or improved errors from pandas IO Excel read_excel, to_excel Regression Functionality that used to work in a prior pandas version Upstream issue Issue related to pandas dependency and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 18, 2021
@simonjayhawkins simonjayhawkins added this to the 1.2.1 milestone Jan 18, 2021
@asishm
Copy link
Contributor

asishm commented Jan 18, 2021

@simonjayhawkins The regression is because of the default engine being changed to openpyxl

in 1.1.5, specifying engine='openpyxl' leads to the same error.

There are many such issues where changing the engine has caused issues. #38956 #39001 and (#34747 (comment) on #34747)

@simonjayhawkins
Copy link
Member

just to be clear

@simonjayhawkins The regression is because of the default engine being changed to openpyxl

and this is unlikely to be changed. so any code that worked in 1.1.5 and does not in 1.2 should be considered a regression in the first instance (even if an upstream issue)

in 1.1.5, specifying engine='openpyxl' leads to the same error.

and the converse is also true, specifying engine='xlrd' in 1.2 and the code should work fine.

There are many such issues where changing the engine has caused issues. #38956 #39001 and (#34747 (comment) on #34747)

these should probably be labelled as regressions also (will look at these shortly)

@simonjayhawkins
Copy link
Member

There are many such issues where changing the engine has caused issues. #38956 #39001 and (#34747 (comment) on #34747)

these should probably be labelled as regressions also (will look at these shortly)

labelled #38956 and #39001 as regressions. For #34747, or indeed any issue with the openpyxl engine, since the default has changed could result in a regression from a user POV, we should consider backporting any fixes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Error Reporting Incorrect or improved errors from pandas IO Excel read_excel, to_excel Regression Functionality that used to work in a prior pandas version Upstream issue Issue related to pandas dependency
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants