Skip to content
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: pd.read_excel with engine "xlrd" fails when a cell contains NaN/inf #54564

Closed
3 tasks done
burnpanck opened this issue Aug 15, 2023 · 4 comments · Fixed by #54567
Closed
3 tasks done

BUG: pd.read_excel with engine "xlrd" fails when a cell contains NaN/inf #54564

burnpanck opened this issue Aug 15, 2023 · 4 comments · Fixed by #54567
Labels
Bug IO Excel read_excel, to_excel IO SQL to_sql, read_sql, read_sql_query Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Milestone

Comments

@burnpanck
Copy link
Contributor

burnpanck commented Aug 15, 2023

Pandas version checks

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of pandas.
  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import tempfile
from pathlib import Path

import numpy as np
import pandas as pd
# xlwt is just needed to conveniently create a test-case here
import xlwt

inp = np.r_[:3,np.nan].reshape(2,2)
print(d)
wb = xlwt.Workbook()
ws = wb.add_sheet('Sheet')
for (i,j),v in np.ndenumerate(inp):
    ws.write(i,j,v)
with tempfile.TemporaryDirectory() as tdir:
    fn = str(Path(tdir)/".xls")
    wb.save(fn)
    out = pd.read_excel(fn, engine="xlrd")

Issue Description

Note that a very similar issue was reported previously: #33853.
However, that one relaed to the openpyxl engine and was thus closed as to be fixed upstream.
It appears that pandas does however vendor xlrd (and there is no issue tracker on the xlrd repo), so I believe this issue belongs here.

pd.read_excel with engine="xlrd" (as required to read xls files) fails when the input document contains infinity values:

...
File ~/.pyenv/versions/3.11.3/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandas/io/excel/_xlrd.py:109, in XlrdReader.get_sheet_data.<locals>._parse_cell(cell_contents, cell_typ)
    105     cell_contents = bool(cell_contents)
    106 elif cell_typ == XL_CELL_NUMBER:
    107     # GH5394 - Excel 'numbers' are always floats
    108     # it's a minimal perf hit and less surprising
--> 109     val = int(cell_contents)
    110     if val == cell_contents:
    111         cell_contents = val

ValueError: cannot convert float NaN to integer

Expected Behavior

pd.read_excel should succeed.

Installed Versions

INSTALLED VERSIONS

commit : 0f43794
python : 3.11.3.final.0
python-bits : 64
OS : Darwin
OS-release : 22.5.0
Version : Darwin Kernel Version 22.5.0: Thu Jun 8 22:22:20 PDT 2023; root:xnu-8796.121.3~7/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : de_CH.UTF-8
LANG : en_GB.UTF-8
LOCALE : de_CH.UTF-8

pandas : 2.0.3
numpy : 1.23.5
pytz : 2023.3
dateutil : 2.8.2
setuptools : 65.5.0
pip : 23.1.2
Cython : 0.29.34
pytest : 7.3.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 3.1.2
lxml.etree : None
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.12.0
pandas_datareader: None
bs4 : 4.12.2
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.7.1
numba : 0.57.0
numexpr : 2.8.4
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : 11.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.10.1
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : 2023.4.2
xlrd : 2.0.1
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

@burnpanck burnpanck added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 15, 2023
@burnpanck burnpanck changed the title BUG: pd.read_excel with engine "xlrd" fails when a cell contains INF BUG: pd.read_excel with engine "xlrd" fails when a cell contains NaN/inf Aug 15, 2023
burnpanck added a commit to burnpanck/pandas that referenced this issue Aug 15, 2023
@burnpanck burnpanck mentioned this issue Aug 15, 2023
5 tasks
@rhshadrach
Copy link
Member

rhshadrach commented Aug 16, 2023

It appears that pandas does however vendor xlrd (and there is no issue tracker on the xlrd repo), so I believe this issue belongs here.

pandas does not vendor xlrd, xlrd is no longer maintained. That is why you can't report issues to xlrd.

@burnpanck
Copy link
Contributor Author

xlrd is no longer maintained

That's what I though.

With the expression "pandas vendors xlrd" I meant that pandas is shipping a copy of xlrd with its own sources (as if pandas were a "vendor" of xlrd). Though I'm not a native english speaker, so I may be using the word incorrectly here. Either way, the fix needs to be done here, and so I guess the issue will remain open here until it's fixed.

@rhshadrach
Copy link
Member

I believe you are using the word correctly. pandas does not do that. If you do not have xlrd installed, you cannot use the xlrd engine with pandas.

"""
err_msg = "Install xlrd >= 2.0.1 for xls Excel support"
import_optional_dependency("xlrd", extra=err_msg)

@burnpanck
Copy link
Contributor Author

Huh. I obviously never read the file completely and just assumed the _xlrd.py to be something copied from the original xlrd library. Thanks for the clarification!

@rhshadrach rhshadrach added Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate IO SQL to_sql, read_sql, read_sql_query IO Excel read_excel, to_excel and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 18, 2023
@rhshadrach rhshadrach added this to the 2.2 milestone Aug 18, 2023
mroeschke pushed a commit that referenced this issue Aug 21, 2023
* added unit-test to highlight issue #54564

* fixed #54564

* added whatsnew entry

* anticipate this fix going into v2.2.0 instead of v2.1.0

* LBYL instead of EAFP

* address review request

* fix tests on windows
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO Excel read_excel, to_excel IO SQL to_sql, read_sql, read_sql_query Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants