Filling missing date values with the least possible date in Pandas dataframe

后端 未结 1 963
春和景丽
春和景丽 2021-01-14 08:42

I have a dataframe with a date column as,

df = pd.DataFrame({\'date\':[\'2014-10-01\', np.nan, \'2015-09-30\', np.nan, np.nan, \'2019-06-03\']})
1条回答
  •  北荒
    北荒 (楼主)
    2021-01-14 09:22

    If you want a date that plays nicely with pandas, you'll need to consider pd.Timestamp, since this is the datetime type that pandas works with.

    If you don't mind your dates having a time component, use pd.Timestamp.min:

    pd.Timestamp.min
    # Timestamp('1677-09-21 00:12:43.145225')
    
    pd.to_datetime(df['date'].fillna(pd.Timestamp.min))
    
    0   2014-10-01 00:00:00.000000
    1   1677-09-21 00:12:43.145225
    2   2015-09-30 00:00:00.000000
    3   1677-09-21 00:12:43.145225
    4   1677-09-21 00:12:43.145225
    5   2019-06-03 00:00:00.000000
    Name: date, dtype: datetime64[ns]
    

    If you only want the dates (without times), then the smallest date sans time component would be

    pd.Timestamp.min.ceil('D')
    # Timestamp('1677-09-22 00:00:00')
    
    pd.to_datetime(df['date'].fillna(pd.Timestamp.min.ceil('D')))
    
    0   2014-10-01
    1   1677-09-22
    2   2015-09-30
    3   1677-09-22
    4   1677-09-22
    5   2019-06-03
    Name: date, dtype: datetime64[ns]
    

    0 讨论(0)
提交回复
热议问题