我在数据框中有许多记录,其中到期日期列是 31-12-9999 12:00:00 AM,因为债券永远不会到期。这自然会引发错误:
Out of bounds nanosecond timestamp: 9999-12-31 00:00:00
我看到最大日期是:
pd.Timestamp.max
Timestamp('2262-04-11 23:47:16.854775807')
我只是想澄清清除 datframe 中所有日期列并修复我的错误的最佳方法是什么?我的代码模仿了文档:
df_Fix_Date = df_Date['maturity_date'].head(8)
display(df_Fix_Date)
display(df_Fix_Date.dtypes)
0 2020-08-15 00:00:00.000
1 2022-11-06 00:00:00.000
2 2019-03-15 00:00:00.000
3 2025-01-15 00:00:00.000
4 2035-05-29 00:00:00.000
5 2027-06-01 00:00:00.000
6 2021-04-01 00:00:00.000
7 2022-04-03 00:00:00.000
Name: maturity_date, dtype: object
def conv(x):
return pd.Period(day = x%100, month = x//100 % 100, year = x // 10000, freq='D')
df_Fix_Date['maturity_date'] = pd.to_datetime(df_Fix_Date['maturity_date']) # convert to datetype
df_Fix_Date['maturity_date'] = pd.PeriodIndex(df_Fix_Date['maturity_date'].apply(conv)) # fix error
display(df_Fix_Date)
输出:
KeyError: 'maturity_date'