简体   繁体   English

Python pandas timedelta64 fillna

[英]Python pandas timedelta64 fillna

I have a dataset with hundreds of columns.我有一个包含数百列的数据集。 Some of the columns have timedelta64 type.一些列具有 timedelta64 类型。 When i use当我使用

fillna(0)

I got an error Passing integers to fillna for timedelta64[ns] dtype is no longer supported. To obtain the old behavior, pass pd.Timedelta(seconds=n)我收到一个错误Passing integers to fillna for timedelta64[ns] dtype is no longer supported. To obtain the old behavior, pass pd.Timedelta(seconds=n) Passing integers to fillna for timedelta64[ns] dtype is no longer supported. To obtain the old behavior, pass pd.Timedelta(seconds=n)

How can i fix this error?我该如何解决这个错误?

You may want to update the columns separately accordingly to their dtypes:您可能希望根据它们的数据类型分别更新列:

# Update inplace numeric columns
df.update(df.select_dtypes('number').fillna(0))

# Update inplace timedelta columns
df.update(df.select_dtypes('timedelta64[ns]').fillna(pd.Timedelta(seconds=0)))

Another possible syntax for updating:另一种可能的更新语法:

df.loc[:, df.dtypes.eq(float)] = df.select_dtypes(float).fillna(0)

As mentioned in the error use:如错误使用中所述:

df['columnname'].fillna(pd.Timedelta(seconds=0))

Passing an integer to Series.fillna() and DataFrame.fillna() with timedelta64[ns] dtypes is deprecated, will raise TypeError in a future version.将 integer 传递给 Series.fillna() 和 DataFrame.fillna() with timedelta64[ns] dtypes 已被弃用,将在未来版本中引发 TypeError。 Use obj.fillna(pd.Timedelta(...))使用 obj.fillna(pd.Timedelta(...))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM