Python - 用sudo日期替换缺少的日期

问题描述 投票:0回答:2

我有一个缺少日期值的数据框,我应该如何用9999-01-01 00:00:00替换它们?

import pandas as pd
df = pd.read_excel('sample-data.xlsx',converters={'sample_date':str})
df['sample_date'] 

output of df['sample_date']:

0    2017-11-08 00:00:00
1    2016-08-03 00:00:00
2    2015-09-29 00:00:00
3    NaT
4    2015-09-29 00:00:00
5    NaT

if df['sample_date'] == "" or df['sample_date'] == None or df['sample_date'] == "NaT" or df['sample_date'] == "NaN":
    df['sample_date'] == "9999-01-01  00:00:00"

我得到的错误如:The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

python pandas
2个回答
1
投票

尝试使用pandas fillna()函数填充数据帧中的NaT值。

df['sample_date'] = df['sample_date'].fillna('9999-01-01 00:00:00')

我不知道这是否适用于NaT值,但如果我的记忆正确,它确实如此。


1
投票

您可能正在寻找.fillna()功能。

df['sample_date'] =df['sample_date'].fillna("9999-01-01  00:00:00")
© www.soinside.com 2019 - 2024. All rights reserved.