Convert integer series to timedelta in pandas

后端 未结 2 1149
滥情空心
滥情空心 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:56

    Just to follow up with joris' response, you can convert an int or a float into whatever time unit you want with pd.to_timedelta(x, unit=''), changing only the entry for unit=:

    # Years, Months, Days:
    pd.to_timedelta(3.5, unit='Y') # returns '1095 days 17:27:36'
    pd.to_timedelta(3.5, unit='M') # returns '91 days 07:27:18'
    pd.to_timedelta(3.5, unit='D') # returns '3 days 12:00:00'
    
    # Hours, Minutes, Seconds:
    pd.to_timedelta(3.5, unit='h') # returns '0 days 03:30:00'
    pd.to_timedelta(3.5, unit='m') # returns '0 days 00:03:30'
    pd.to_timedelta(3.5, unit='s') # returns '0 days 00:00:03.50'
    

    Note that mathematical operations are legal once correctly formatted:

    pd.to_timedelta(3.5, unit='h') - pd.to_timedelta(3.25, unit='h') # returns '0 days 00:15:00'
    

提交回复
热议问题