newbie
newbie

Reputation: 644

Time calculation pandas

I need to calculate the time difference and then assign the results starting from the first row. This is my code:

import pandas as pd

dtime = pd.DataFrame({'letter': ['a', 'b', 'c', 'd', 'e', 'f'], 'time': ['19/11/2019  06:02:39', '19/11/2019  06:32:39', '2019-11-19 06:42:39' , '19/11/2019  06:52:39', '19/11/2019  07:02:39', '19/11/2019  07:12:39']})
dtime['time'] = pd.to_datetime(dtime['time'])

dtime['Time_diff']=(dtime['time']-dtime['time'].shift()).fillna(0)
print (dtime)
dtime.to_excel('test_time.xlsx', 'Sheet1', index=True)

this is the output :

  letter                time Time_diff
0      a 2019-11-19 06:02:39  00:00:00
1      b 2019-11-19 06:32:39  00:30:00
2      c 2019-11-19 06:42:39  00:10:00
3      d 2019-11-19 06:52:39  00:10:00
4      e 2019-11-19 07:02:39  00:10:00
5      f 2019-11-19 07:12:39  00:10:00

but I need it to be this way:

  letter                time Time_diff
0      a 2019-11-19 06:02:39  00:30:00
1      b 2019-11-19 06:32:39  00:10:00
2      c 2019-11-19 06:42:39  00:10:00
3      d 2019-11-19 06:52:39  00:10:00
4      e 2019-11-19 07:02:39  00:10:00
5      f 2019-11-19 07:12:39  00:00:00

row 0: 0 a 2019-11-19 06:02:39 00:30:00

It is possible??

Upvotes: 1

Views: 26

Answers (1)

jezrael
jezrael

Reputation: 863481

Change order and add -1 to Series.shift, also for replace missing values use 0 timedelta:

dtime['Time_diff']=(dtime['time'].shift(-1)-time['time']).fillna(pd.Timedelta(0))
print (dtime)
  letter                time Time_diff
0      a 2019-11-19 06:02:39  00:30:00
1      b 2019-11-19 06:32:39  00:10:00
2      c 2019-11-19 06:42:39  00:10:00
3      d 2019-11-19 06:52:39  00:10:00
4      e 2019-11-19 07:02:39  00:10:00
5      f 2019-11-19 07:12:39  00:00:00

Upvotes: 2

Related Questions