Skip to content

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

Closed
@burnpanck

Description

@burnpanck

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIO Excelread_excel, to_excelIO SQLto_sql, read_sql, read_sql_queryMissing-datanp.nan, pd.NaT, pd.NA, dropna, isnull, interpolate

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions