Convert integer series to timedelta in pandas

后端 未结 2 1233
滥情空心
滥情空心 2021-02-19 09:24

I have a data frame in pandas which includes number of days since an event occurred. I want to create a new column that calculates the date of the event by subtracting the numb

2条回答
  •  轮回少年
    2021-02-19 09:55

    First, to convert the column with integers to a timedelta, you can use to_timedelta:

    In [60]: pd.to_timedelta(df['days_since_event'], unit='D')
    Out[60]:
    0   5 days
    1   7 days
    2   3 days
    3   6 days
    4   0 days
    Name: days_since_event, dtype: timedelta64[ns]
    

    Then you can create a new column with the current date and substract those timedelta's:

    In [62]: df['event_date'] = pd.Timestamp('2015-12-29')
    
    In [63]: df['event_date'] = df['event_date'] -  pd.to_timedelta(df['days_since_event'], unit='D')
    
    In [64]: df['event_date']
    Out[64]:
    0   2015-12-24
    1   2015-12-22
    2   2015-12-26
    3   2015-12-23
    4   2015-12-29
    dtype: datetime64[ns]
    

提交回复
热议问题