【问题标题】:Missing Values in a Time Period Difference Measurement时间段差测量中的缺失值
【发布时间】:2020-05-29 09:58:47
【问题描述】:

我有一些时间戳数据:

df = pd.DataFrame({'t' : [dt.datetime(2016,1,7),float('NaN'),dt.datetime(2016,1,8),float('NaN')],
                   'A' : [0,1,1,1]})

我的一些 t 值是 NaT。我想更改 A 以使其在具有 NaT 的行上为零,如下所示:

我试过了:

# Missing values of time
def MVOT(t,A):
    if math.isnan(t):
        return 0
    else:
        return A
MVOT = np.vectorize(MVOT,otypes=[float])
df["A"] = MVOT(df['t'],df['A'])

但我得到了:

TypeError: must be real number, not NoneType

【问题讨论】:

    标签: python pandas timestamp nan missing-data


    【解决方案1】:

    将时间转换为数字

    df['t'] = pd.to_numeric(df.t)
    

    将 NaT 值转换为负数,因此:

    df.loc[df.t < 0,'A'] = 0
    

    【讨论】:

      猜你喜欢
      • 2023-02-24
      • 2018-12-22
      • 1970-01-01
      • 1970-01-01
      • 2019-10-22
      • 1970-01-01
      • 2018-08-24
      • 1970-01-01
      相关资源
      最近更新 更多