计算重叠时间戳差异和重复时间的总和

问题描述 投票:0回答:1

我有一个包含两列的数据框,其中包含会话的开始和结束时间戳:

1 2017-12-04 05:26:15   2017-12-04 05:28:39
2 2017-12-04 05:26:31   2017-12-04 05:34:36
3 2017-12-04 05:28:44   2017-12-04 05:28:54
4 2017-12-04 05:29:05   2017-12-04 05:30:57
5 2017-12-04 05:31:04   2017-12-04 05:31:13
6 2017-12-04 05:31:06   2017-12-04 05:31:13
7 2017-12-04 05:31:26   2017-12-04 05:43:18
8 2017-12-04 05:33:31   2017-12-04 05:35:54
9 2017-12-04 05:34:12   2017-12-04 05:35:24

我需要计算这些会话的差异(即持续时间)的总和,不包括重复时间和重复时间。例如:考虑会话7,8,9。从05:31:26(会议7)开始,到05:43:18(会议7)结束。但在两者之间有多个会话开放。

7,8,9的结果将是:不包括重复时间的持续时间:11分52秒重复时间:2分23秒(会话8)+ 1分12秒(会话9)= 3分35秒

如何在python中实现这一点?

python pandas timestamp
1个回答
0
投票

对不起,我很抱歉。同时找出解决方案 - 不确定它是否是最有效的解决方案,但它确实有效。

for i in range(0,len(inter_data)):
### PLEASE INDENT HERE WHEN USING THE CODE ###

if i == 0:
    begin_first = inter_data.iloc[i]['begin_timestamp']
    end_first = inter_data.iloc[i]['end_timestamp']
    dupl = 0
    dur = (end_first-begin_first).seconds
else:
    begin = inter_data.iloc[i]['begin_timestamp']    
    end = inter_data.iloc[i]['end_timestamp']

    if begin >= end_first:
        dur = dur + (end-begin).seconds
        end_first = end
    elif (begin < end_first) and (end >= end_first) :
        dur = dur + (end-end_first).seconds
        dupl = dupl + (end_first-begin).seconds
        end_first = end
    elif (begin <= end_first) and (end < end_first):
        dupl = dupl + (end-begin).seconds
© www.soinside.com 2019 - 2024. All rights reserved.