Emm
Emm

Reputation: 2507

Subtract value in current row with previous rows values and return the greater value

I would like to subtract the start date in the current row from the end and error date and return the largest timedelta

          start                end       error_time
12239   2019-02-18 00:15:13 2019-02-18 01:07:41     NaT
12241   2019-02-18 01:07:56 2019-02-18 01:17:07     NaT
12243   2019-02-18 13:29:51 2019-02-18 13:41:17     NaT
12775   2019-02-18 21:31:27 2019-02-18 23:06:26     NaT
12777   2019-02-18 23:06:57 2019-02-18 23:14:38     NaT
12778   2019-02-19 09:09:51       NaT              2019-02-19 09:10:53
12780   2019-02-19 08:22:57 2019-02-19 23:04:37     NaT
12781   2019-02-19 23:04:37 2019-02-19 23:17:04     NaT
12782   2019-02-20 15:40:11 2019-02-20 15:42:27    2019-03-12 12:00:48

I can already subtract the start date from the previous end date but not sure how to go about comparing this number with the timedelta for start - error and returning the greater of the two values. I tried using if else statements but that gives me the following error message:

The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Code I have:

a = br['start'] - br['end'].shift(1)

Upvotes: 0

Views: 42

Answers (1)

Ali Hallaji
Ali Hallaji

Reputation: 4392

you can compare two timedelta objects like this:

import datetime

a = datetime(2019,3,3,12,12,12) #start time 1
b = datetime(2020,3,3,12,12,12) #end time 1
c = datetime(2019,3,4,12,12,12) #start time 2
d = datetime(2020,3,2,12,12,12) #end time 2
delta1 = a - b
delta2 = c - d
print(delta1 > delta2) # print False
print(delta1 < delta2) # print True

you dont need to use any special method.

Upvotes: 1

Related Questions