我希望更新任何晚于Python的最大日期的日期:
display(df['maturity_date'].dtypes)
dtype('O')
display(df['maturity_date'].describe())
count 3488
unique 766
top 2166-09-23 00:00:00
freq 130
Name: maturity_date, dtype: object
m = df['maturity_date'].str[:4].astype(int) > 2261
ValueError: cannot convert float NaN to integer
在这种情况下,似乎没有错误,所以这是引发错误的原因吗?我如何才能使Pandas仅在条件为真时才更新?最好是抑制错误还是处理它?
df['maturity_date'] = df['maturity_date'].mask(m, '2261' + df['maturity_date'].str[4:]) # for all dates greater than python max date replace
df['maturity_date'] = pd.to_datetime(df['maturity_date'])
发布于 2018-05-28 18:32:30
我认为需要转换为float
,因为缺少的NaN
s或None
s的值具有类型float
,因为在pandas中不可能根据设计将NaN
s转换为int
:
m = df['maturity_date'].str[:4].astype(float) > 2261
另一种解决方案是首先使用NaN
s或None
s删除行:
df = df.dropna(subset=['maturity_date'])
对于检查有问题的行,请使用:
df = df[df['maturity_date'].isnull()]
https://stackoverflow.com/questions/50564331
复制相似问题