[英]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 我想从结束日期和错误日期中减去当前行的开始日期,并返回最大的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. 我已经可以从上一个结束日期中减去开始日期,但是不确定如何将该数字与timedelta进行比较-错误并返回两个值中的较大者。 I tried using if else statements but that gives me the following error message: 我尝试使用if else语句,但这会给我以下错误消息:
The truth value of a Series is ambiguous. 系列的真实值是不明确的。 Use a.empty, a.bool(), a.item(), a.any() or a.all(). 使用a.empty,a.bool(),a.item(),a.any()或a.all()。
Code I have: 代码我有:
a = br['start'] - br['end'].shift(1)
you can compare two timedelta objects like this: 您可以像这样比较两个timedelta对象:
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. 您不需要使用任何特殊方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.