Reputation: 66565
I would like to calculate hours difference between two consecutive index values, similarly to rows.
In the case of rows, I did the following:
diffs_a = df['TotalWorkingHours'].rolling(window=2).apply(lambda x: x[1] - x[0])
and got the following result:
DateTime
2018-11-16 14:30:31+00:00 NaN
2018-11-16 14:30:41+00:00 0.00
2018-11-16 14:37:21+00:00 0.00
2018-11-16 14:37:31+00:00 0.00
2018-11-16 14:37:41+00:00 0.00
...
2020-04-25 06:28:54+00:00 0.00
2020-04-25 06:29:04+00:00 0.01
2020-04-25 06:29:14+00:00 0.00
2020-04-25 06:29:24+00:00 0.00
2020-04-25 06:29:34+00:00 0.00
Basically what I would like to do is to calculate time difference in hours as well and compare (treat the same) these differences to the working hours differences something like the following (which is not correctly defined):
diffs_time = df.index.rolling(window=2).apply(lambda x: x[1] - x[0])
Upvotes: 0
Views: 112
Reputation: 260335
Try to reset_index
and use the diff
method on the "DateTime" column:
df.reset_index()["DateTime"].diff()
Upvotes: 1