早上好,
我需要在我的档案中规定超过2261个日期。在执行以下操作时,我会得到上面的错误:
m = df['maturity_date'].str[:4].astype(int) > 2261
ValueError: cannot convert float NaN to integer引起错误的列的详细信息:
display(df['maturity_date'].dtypes)
dtype('O')
display(df['maturity_date'].describe())
count 3709
unique 781
top 2166-09-23 00:00:00.000
freq 234
Name: maturity_date, dtype: object
display(df[df['maturity_date'].isnull()])
No records returned
display(df[df['maturity_date']==0]['maturity_date'] )
764 0
931 0
1173 0
Name: maturity_date, dtype: object可能会引发错误,因为您不能转换零?我打算在它运行后更新日期的代码:
#Convert dates greater than 2261
display(df['maturity_date'].str[:4].astype(int) > 2261)
df['maturity_date'] = df['maturity_date'].mask(m, '2261' + to df['maturity_date'].str[4:]) # for all dates greater than python max date replace
df['maturity_date'] = pd.to_datetime(df['maturity_date']) 发布于 2018-04-27 03:41:12
这应该能行。您需要首先将整数转换为字符串,以便可以使用.str方法。
m = df['maturity_date'].astype('str').str[:4].astype(int) > 2261问题是,如果您不想调用整数值的.str。当您这样做时,它会将其转换为NaN,这将使您在转换为整数时遇到问题。
例如:
import pandas as pd
df = pd.DataFrame({'value': [0, '0', '0000', '000']})
df.value.str[:4]
#0 NaN
#1 0
#2 0000
#3 000
#Name: value, dtype: object
df.value.astype('str').str[:4]
#0 0
#1 0
#2 0000
#3 000
#Name: value, dtype: objecthttps://stackoverflow.com/questions/50054642
复制相似问题