另一栏中的pandas fillna日期

时间:2016-10-06 14:59:50

标签: python date datetime pandas data-manipulation

获取我的测试数据:

 import pandas as pd

 df =    {'Id': {1762056: 2.0, 1762055: 1.0},
'FillDate': {1762056: Timestamp('2015-08-01 00:00:00'), 1762055:Timestamp('2015-08-01 00:00:00')}, 
 'Date': {1762056: nan, 1762055: nan}, 
 }

df = pd.DataFrame(df)

数据如下:

        Id         Date        FillDate
        1.0      NaN         2015-08-01
        2.0      NaN         2015-08-01

所以为了填补缺少的日期,我这样做:

df['Date'].fillna(df['FillDate'], inplace=True)

给了我

         Id    Date                  FillDate
        1.0  1438387200000000000  2015-08-01
        2.0  1438387200000000000  2015-08-01

如何在日期格式中获取Date

1 个答案:

答案 0 :(得分:4)

这有效:

 df['Date'].fillna(pd.to_datetime(df['FillDate']).dt.date, inplace=True)

给出了

      Id    Date       FillDate
      1.0  2015-08-01  2015-08-01
      2.0  2015-08-01  2015-08-01