Pandas: group with TimeGrouper
IIUC you need:
print (df.groupby('ID')['used_at'].diff().dt.seconds)0 NaN1 33.02 54.03 34.04 4.05 4.06 8.07 16.08 6.0Name: used_at, dtype: float64
If you wish to use TimeGrouper
, you should first set a Datetimeindex
and then you can use any aggregating function - e.g. sum
:
df['used_at'] = pd.to_datetime(df.used_at)df.set_index('used_at', inplace=True)print (df.groupby([df['ID'],pd.TimeGrouper(freq='5Min')]).sum())
Another way to do it is to copy the column used_at
to index
:
df['used_at'] = pd.to_datetime(df.used_at)df.set_index(df['used_at'], inplace=True)print (df.groupby([df['ID'], df['used_at'],pd.TimeGrouper(freq='5Min')]).sum())