Pandas read_excel() parses date columns with blank values to NaT

谁都会走 提交于 2021-02-08 10:50:47

问题


I am trying to read an excel file that has date columns with the below code

src1_df = pd.read_excel("src_file1.xlsx", keep_default_na = False)

Even though I have specified, keep_default_na = False, I see that the data frame has 'NaT' value(s) for corresponding blank cells in Excel date columns.

Please suggest, how to get a blank string instead of 'NaT' while parsing Excel files.

I am using Python 3.x and Pandas 0.23.4


回答1:


src1_df = pd.read_excel("src_file1.xlsx", na_filter=False)

Then you will have empty string ("") as "na" value

In my case I read excel per line and replace "" and "NaT" to None:

for line in src1_df.values:
    for index, value in enumerate(line):
        if value == '' or isinstance(value, pd._libs.tslibs.nattype.NaTType):
            line[index] = None
dostuff_with(line)


来源:https://stackoverflow.com/questions/53304800/pandas-read-excel-parses-date-columns-with-blank-values-to-nat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!