Add duration with n days hh:mm:ss format in python

Viewed 28

I have set of data with the format n days hh:mm:ss. I tried to use this code since I am adding duration based on its id:

groupby(['event_id'])['data_ts'].sum()

but as expected, it produced the error

TypeError: datetime64 type does not support add operations

Is there a way to add data with this format in python?

1 Answers

If convert values to timedeltas solution working well:

df = pd.DataFrame({'event_id':[1,2,2,3,3,3],
                   'data_ts':['1 days 10:01:02','0 days 10:01:02',
                              '1 days 00:01:02','0 days 00:41:02',
                              '1 days 10:01:02','1 days 10:01:02']})

df['data_ts'] = pd.to_timedelta(df['data_ts'])

s = df.groupby(['event_id'])['data_ts'].sum()
print (s)
event_id
1   1 days 10:01:02
2   1 days 10:02:04
3   2 days 20:43:06
Name: data_ts, dtype: timedelta64[ns]
Related