python - How to get the duration inside the rolling window from he DatetimeIndex in Pandas -
i trying calculate time duration inside of each sliding window 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 example date index, , trying duration inside rolling window of size 3 overlap each other. answer should 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 got error:
typeerror: 'datetimeindex' object not callable
df.reset_index(inplace=true) df['previous_time']= df.date.shift(-2) df['duration']=(df.previous_time-df.date)/np.timedelta64(1,'s') df.drop('previous_time',axis=1,inplace=true) df.set_index('date',inplace=true) assuming 'date' datetime.
Comments
Post a Comment