【问题标题】:Pandas filling missing date values with a constant date熊猫用固定日期填充缺失的日期值
【发布时间】:2021-07-15 11:56:56
【问题描述】:

我有一种情况,我试图使用标准日期在日期列中估算缺失值。我正在使用以下代码,但缺失的值仍然保持原样,不会被我使用的日期替换。

df:

termination_date
2020-06-28 00:00:00

2020-07-13 00:00:00
2020-08-11 00:00:00

2020-08-11 00:00:00

现在要替换缺失值,我想使用日期 '2020-07-31 00:00:00',并且我正在使用以下代码:

df['termination_date'] = df['termination_date'].fillna(value=pd.to_datetime('2020-07-31 00:00:00'))

输出应该是这样的:

termination_date
2020-06-28 00:00:00
2020-07-31 00:00:00
2020-07-13 00:00:00
2020-08-11 00:00:00
2020-07-31 00:00:00
2020-08-11 00:00:00

【问题讨论】:

    标签: pandas date missing-data imputation


    【解决方案1】:

    将值转换为日期时间,而非日期时间为NaT,因此可能替换为fillna

    df['termination_date'] = (pd.to_datetime(df['termination_date'], errors='coerce')
                                .fillna(pd.to_datetime('2020-07-31')))
    
    #because same times 00:00:00 are not shown
    print (df)
      termination_date
    0       2020-06-28
    1       2020-07-31
    2       2020-07-13
    3       2020-08-11
    4       2020-07-31
    5       2020-08-11
    
    print(df['termination_date'].tolist())
    [Timestamp('2020-06-28 00:00:00'), Timestamp('2020-07-31 00:00:00'),
     Timestamp('2020-07-13 00:00:00'), Timestamp('2020-08-11 00:00:00'), 
     Timestamp('2020-07-31 00:00:00'), Timestamp('2020-08-11 00:00:00')]
    
    print (df.termination_date.dtypes)
    datetime64[ns]
    

    【讨论】:

      【解决方案2】:

      来自您的DataFrame

      >>> df = pd.DataFrame({'termination_date': ["2020-06-28 00:00:00",
      ...                                         "",
      ...                                         "2020-07-13 00:00:00",
      ...                                         "2020-08-11 00:00:00",
      ...                                         "",
      ...                                         "2020-08-11 00:00:00"]}, 
      ...                   index = [0, 1, 2, 3, 4, 5])
      >>> df
          termination_date
      0   2020-06-28 00:00:00
      1   
      2   2020-07-13 00:00:00
      3   2020-08-11 00:00:00
      4   
      5   2020-08-11 00:00:00
      

      我们可以使用loc 将缺失值替换为pd.to_datetime('2020-07-31 00:00:00') 以获得预期结果:

      >>> df.loc[df['termination_date'] == '', 'termination_date'] = pd.to_datetime('2020-07-31 00:00:00')
      >>> df
          termination_date
      0   2020-06-28 00:00:00
      1   2020-07-31 00:00:00
      2   2020-07-13 00:00:00
      3   2020-08-11 00:00:00
      4   2020-07-31 00:00:00
      5   2020-08-11 00:00:00
      

      最后,我们可以将列转换为Datetime 格式,以确保我们没有string 值:

      df['termination_date'] = pd.to_datetime(df['termination_date'])
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-10
        • 1970-01-01
        • 2017-12-12
        • 1970-01-01
        • 1970-01-01
        • 2018-04-24
        相关资源
        最近更新 更多