I am trying to calculate time duration inside of each sliding window for this data:
ID
DATE
2017-05-17 15:49:51 2
2017-05-17 15:49:52 5
2017-05-17 15:49:55 2
2017-05-17 15:49:56 3
2017-05-17 15:49:58 5
2017-05-17 15:49:59 5
In this example DATE
is the index, and I am trying to get the duration inside rolling window of size 3 which overlap each other. Answer should be like this:
ID duration
DATE
2017-05-17 15:49:51 2 4
2017-05-17 15:49:52 5 4
2017-05-17 15:49:55 2 3
2017-05-17 15:49:56 3 3
2017-05-17 15:49:58 5 NaN
2017-05-17 15:49:59 5 NaN
I tried:
df['duration'] = df.rolling(window=3).apply(df.index.max()-df.index.min())
But I got this error:
TypeError: 'DatetimeIndex' object is not callable