Skip to content

BUG: read_excel with dtype=np.int32 can't handle empty or nan cells #34662

Closed
@joha1

Description

@joha1

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

Code Sample, a copy-pastable example

EDIT:
(won't let me comment)
Apparently, it's possible with dtype='Int64' (capital I and in ticks).
https://pandas.pydata.org/pandas-docs/version/0.24/whatsnew/v0.24.0.html#optional-integer-na-support
Unfortunately, that's not very well documented. https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_excel.html makes it sound like you're supposed to give it a numpy type.
So it's more of a documentation issue, instead of a bug
/EDIT

# save the lines below as test.xls:

ID  string
1   a
2   b
3   c
    d
4   e
nan f
6   g

# code:
test = pd.read_excel('test.xls')
test
    ID string
0  1.0      a
1  2.0      b
2  3.0      c
3  NaN      d
4  4.0      e
5  NaN      f
6  6.0      g

# note that the IDs gained a decimal! They're needed as an ID to retrive some files, to that 0 is breaking things. So let's import `ID` as integer:

test = pd.read_excel('test.xls', dtype={'ID':np.int32})        

ValueError                                Traceback (most recent call last)
...
ValueError: Cannot convert non-finite values (NA or inf) to integer
...
ValueError: Unable to convert column ID to type <class 'numpy.int32'>

# similar things happen with the nan cell.

Problem description

Importing an excel file with nan or in a column of integers fails, when specifying this column as dtype=np.int32

Expected Output

setting nan or as np.nan or similar or having a documented workaround in the documentation. na_filter=False seems promising, but doesn't do anything.

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit : None python : 3.7.7.final.0 python-bits : 64 OS : Linux OS-release : 4.18.0-147.8.1.el8_1.x86_64 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8

pandas : 1.0.3
numpy : 1.18.1
pytz : 2020.1
dateutil : 2.8.1
pip : 20.0.2
setuptools : 47.1.1.post20200604
Cython : 0.29.17
pytest : 5.4.2
hypothesis : 5.5.4
sphinx : 3.0.4
blosc : None
feather : None
xlsxwriter : 1.2.8
lxml.etree : 4.5.0
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.13.0
pandas_datareader: None
bs4 : 4.9.1
bottleneck : 1.3.2
fastparquet : None
gcsfs : None
lxml.etree : 4.5.0
matplotlib : 3.1.3
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.3
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 5.4.2
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : 1.3.17
tables : 3.6.1
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : 1.3.0
xlsxwriter : 1.2.8
numba : 0.49.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions