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
df['duration'] = df.rolling(window=3).apply(lambda x: x.index.max()-x.index.min())AttributeError: 'numpy.ndarray' object has no attribute 'index'df['duration'] = df.rolling(5).apply(lambda x: pd.to_datetime(x.index.max()) - pd.to_datetime(x.index.min()))Got the same errorAttributeError: 'numpy.ndarray' object has no attribute 'index'rollingworks on a numpy array, not a dataframe, so you do not have access to all the pandas functionality inside. You have to find a workaround based on array-indexing.