'NaTType' object has no attribute 'days'

左心房为你撑大大i 提交于 2019-12-12 12:20:01

问题


I have a column in my dataset which represents a date in ms and sometimes its values is nan (actually my columns is of type str and sometimes its valus is 'nan'). I want to compute the epoch in days of this column. The problem is that when doing the difference of two dates:

(pd.to_datetime('now') - pd.to_datetime(np.nan)).days

if one is nan it is converted to NaT and the difference is of type NaTType which hasn't the attribute days.

In my case I would like to have nan as a result.

Other approach I have tried: np.datetime64 cannot be used, since it cannot take as argument nan. My data cannot be converted to int since int doesn't have nan.


回答1:


It will just work even if you filter first:

In [201]:
df = pd.DataFrame({'date':[dt.datetime.now(), pd.NaT, dt.datetime(2015,1,1)]})
df

Out[201]:
                        date
0 2015-08-28 12:12:12.851729
1                        NaT
2 2015-01-01 00:00:00.000000

In [203]:
df.loc[df['date'].notnull(), 'days'] = (pd.to_datetime('now') - df['date']).dt.days
df

Out[203]:
                        date  days
0 2015-08-28 12:12:12.851729    -1
1                        NaT   NaN
2 2015-01-01 00:00:00.000000   239



回答2:


For me upgrading to pandas 0.20.3 from pandas 0.19.2 helped resolve this error.

pip install --upgrade pandas


来源:https://stackoverflow.com/questions/32269676/nattype-object-has-no-attribute-days

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