nattype.NaTType无法识别NaT值

时间:2019-07-08 09:49:45

标签: python pandas

如果是NaT,我正在尝试为mean_mat_id分配一个新值。但是,在这种情况下,python无法识别出这种情况。

我该怎么办?

mean_mat_id=(luigi['maturity']-luigi['day']).mean()
if mean_mat_id==np.datetime64('NaT'):
    mean_mat_id=mean_mat

编辑 这是最少的代表。码: (可以在此处下载rep.csv:https://www.dropbox.com/s/pyagjwh35ndz9lu/rep.csv?dl=0

import pandas as pd
import numpy as np
import os

master=pd.read_csv('rep.csv', index_col=[0],low_memory=False)
master['day']=pd.to_datetime(master['day'],format='%Y-%m-%d')
master['maturity']=pd.to_datetime(master['maturity'],format='%Y-%m-%d')

mean_mat=(master.maturity-master.day).mean()

for i in master.id.unique():
    luigi=master[master.id==i]

    luigi['day']=pd.to_datetime(luigi['day'],format='%Y-%m-%d')
    luigi['maturity']=pd.to_datetime(luigi['maturity'],format='%Y-%m-%d')

    mean_mat_id=(luigi['maturity']-luigi['day']).mean()
    if mean_mat_id==np.datetime64('NaT'):
#    if type(mean_mat_id)==type(pd.NaT):
        mean_mat_id=mean_mat

1 个答案:

答案 0 :(得分:0)

np.isnan()不适用于NaT,但pd.isna()可以。使用if pd.isna(mean_mat_id):