问题在于 pandas maximal Timestamp 是:
print (pd.Timestamp.max)
2262-04-11 23:47:16.854775807
所以在 pandas 中会引发错误:
print (pd.to_datetime('9999-12-31'))
OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 9999-12-31 00:00:00
示例:
df1 = pd.DataFrame({'eventDate0142': [np.nan, np.nan, '2016-04-01'],
'statusDateTi': [np.nan, '2019-01-01', '2017-04-01']})
df3 = df1.apply(pd.to_datetime)
print (df3)
eventDate0142 statusDateTi
0 NaT NaT
1 NaT 2019-01-01
2 2016-04-01 2017-04-01
可能的解决方案是使用纯 python,但是所有 pandas datetimelike 方法都失败了 - 所有数据都转换为 dates:
from datetime import date
print (date.fromisoformat('9999-12-31'))
9999-12-31
df3['check_date'] = (df3['eventDate0142'].dt.date
.fillna(df3['statusDateTi'].dt.date
.fillna(date.fromisoformat('9999-12-31'))))
print (df3)
eventDate0142 statusDateTi check_date
0 NaT NaT 9999-12-31
1 NaT 2019-01-01 2019-01-01
2 2016-04-01 2017-04-01 2016-04-01
print (df3.dtypes)
eventDate0142 datetime64[ns]
statusDateTi datetime64[ns]
check_date object
dtype: object
或者通过Series.dt.to_period将时间戳转换为每日周期,然后将Periods用于representing out of bounds spans:
print (pd.Period('9999-12-31'))
9999-12-31
df3['check_date'] = (df3['eventDate0142'].dt.to_period('d')
.fillna(df3['statusDateTi'].dt.to_period('d')
.fillna(pd.Period('9999-12-31'))))
print (df3)
eventDate0142 statusDateTi check_date
0 NaT NaT 9999-12-31
1 NaT 2019-01-01 2019-01-01
2 2016-04-01 2017-04-01 2016-04-01
print (df3.dtypes)
eventDate0142 datetime64[ns]
statusDateTi datetime64[ns]
check_date period[D]
dtype: object
如果分配回所有列:
df3['eventDate0142'] = df3['eventDate0142'].dt.to_period('d')
df3['statusDateTi'] = df3['statusDateTi'].dt.to_period('d')
df3['check_date'] = (df3['eventDate0142']
.fillna(df3['statusDateTi']
.fillna(pd.Period('9999-12-31'))))
print (df3)
eventDate0142 statusDateTi check_date
0 NaT NaT 9999-12-31
1 NaT 2019-01-01 2019-01-01
2 2016-04-01 2017-04-01 2016-04-01
print (df3.dtypes)
eventDate0142 period[D]
statusDateTi period[D]
check_date period[D]
dtype: object