cookiedookie cookiedookie - 6 months ago 19
Python Question

How do I generate a TimedeltaIndex from a column of timestamps where the each TimeDelta is generated with reference to the previous timestamp

I have a list of timestamps:

Timestamp

2015-10-01 00:00:08
2015-10-01 00:00:18
2015-10-01 00:00:28
2015-10-01 00:00:38
2015-10-01 00:00:48
2015-10-01 00:00:58
2015-10-01 00:01:08
2015-10-01 00:01:17


and I want to generate TimeDeltas from the timestamp where each TimeDelta value takes reference from the previous timestamp like this:

Timestamp TimeDelta

2015-10-01 00:00:08 00:00:00.000
2015-10-01 00:00:18 00:00:10.000
2015-10-01 00:00:28 00:00:10.000
2015-10-01 00:00:38 00:00:10.000
2015-10-01 00:00:48 00:00:10.000
2015-10-01 00:00:58 00:00:10.000
2015-10-01 00:01:08 00:00:10.000
2015-10-01 00:01:17 00:00:09.000


But instead i'm getting this:

Timestamp TimeDelta

2015-10-01 00:00:08 00:00:00.000
2015-10-01 00:00:18 00:00:10.000
2015-10-01 00:00:28 00:00:20.000
2015-10-01 00:00:38 00:00:30.000
2015-10-01 00:00:48 00:00:40.000
2015-10-01 00:00:58 00:00:50.000
2015-10-01 00:01:08 00:01:00.000
2015-10-01 00:01:17 00:01:09.000


I'm using the following code:

df['TimeDelta'] = pd.TimedeltaIndex(df['Timestamp'])


Thank you!

Answer

Use the diff method to compute the difference of adjacent rows:

df['TimeDelta'] = df['Timestamp'].diff().fillna(0)

import pandas as pd

df = pd.DataFrame({'Timestamp': ['2015-10-01 00:00:08', '2015-10-01 00:00:18',
   '2015-10-01 00:00:28', '2015-10-01 00:00:38', '2015-10-01 00:00:48',
   '2015-10-01 00:00:58', '2015-10-01 00:01:08', '2015-10-01 00:01:17']})
df['Timestamp'] = pd.to_datetime(df['Timestamp'])
df['TimeDelta'] = df['Timestamp'].diff().fillna(0)
print(df)

yields

            Timestamp  TimeDelta
0 2015-10-01 00:00:08   00:00:00
1 2015-10-01 00:00:18   00:00:10
2 2015-10-01 00:00:28   00:00:10
3 2015-10-01 00:00:38   00:00:10
4 2015-10-01 00:00:48   00:00:10
5 2015-10-01 00:00:58   00:00:10
6 2015-10-01 00:01:08   00:00:10
7 2015-10-01 00:01:17   00:00:09