Warm tip: This article is reproduced from serverfault.com, please click

Pandas Python

发布于 2020-11-29 13:21:19

I would like to make a subtraction with date_time in pandas python but with a shift of two rows, I don't know the function

Timestamp
2020-11-26 20:00:00
2020-11-26 21:00:00
2020-11-26 22:00:00
2020-11-26 23:30:00

Explanation:

(2020-11-26 21:00:00) - (2020-11-26 20:00:00)
(2020-11-26 23:30:00) - (2020-11-26 22:00:00)

The result must be:

01:00:00
01:30:00
Questioner
Thony Nadhir
Viewed
0
Dani Mesejo 2020-11-29 21:27:58

IIUC, you want to iterate on chunks of two and find the difference, one approach is to:

res = df.groupby(np.arange(len(df)) // 2).diff().dropna()
print(res)

Output

        Timestamp
1 0 days 01:00:00
3 0 days 01:30:00