简体   繁体   中英

ValueError: cannot convert float NaN to integer at read excel

I am reading an xlsx file that beforehand every time I try to open it the window with the message pops up:

We found a problem with some content in '...xlsx'. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click yes

I have tried opening the file with pandas as follows::

df = pd.read_excel(path_xlsx)

or

df = pd.read_excel(path_xlsx, dtype=str)


>>ValueError: cannot convert float NaN to integer

I suspect the column in question is a named "ExpDate"... it contains data of this style:

NaN
NaN
NaN
31/12/2021
31/12/2021
NaN

I can't manipulate the file manually

Interesting one!

even though parse_dates is set to False, it still tries to parse the dates, and it's mainly driven from the column type which is set up in excel. One way would be if there issue with or you suspect an issue with any date column, change that column to text in excel before asking pandas to read.

import pandas as pd
file_name="Inventory Lots_20210423142831(1).xlsx"
df=pd.read_excel(file_name,header=0,parse_dates=False)

workaround on this problem, pls clean the dates in dataclean up and then import through pandas.

Result: Result

exception trace:

67                     cell_contents = xldate.xldate_as_datetime(cell_contents, epoch1904)
# then python3.8/site-packages/xlrd/xldate.py in xldate_as_datetime(xldate, datemode)
    150     # The integer part of the Excel date stores the number of days since
    151     # the epoch and the fractional part stores the percentage of the day.
--> 152     days = int(xldate)

I had the exact same problem and got it sorted out by repairing the file (going ahead with "recover as much as we can" pop up) and saving the fixed excel file as a new one and reading data from that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM