如何从csv获取具有开始时间和结束时间的两个日期范围之间的重叠?

时间:2019-03-26 19:03:03

标签: python pandas datetime

我已经问过类似的问题,但可以将更多问题拼凑在一起,但需要更多帮助。 Determining how one date/time range overlaps with the second date/time range? 我希望能够检查开始日期/时间和结束日期/时间的两个日期范围是否重叠。我的type2大约有50行,而type 1则超过500行。我希望能够采用type2的开头和结尾,并查看它是否在type1范围内。这是一小段数据,但是日期从第二天的2019-04-01起确实从列表中向下改变。

    type1 type1_start                type1_end
    a    2019-04-01T00:43:18.046Z    2019-04-01T00:51:35.013Z
    b    2019-04-01T02:16:46.490Z    2019-04-01T02:23:23.887Z
    c    2019-04-01T03:49:31.981Z    2019-04-01T03:55:16.153Z
    d    2019-04-01T05:21:22.131Z    2019-04-01T05:28:05.469Z

    type2 type2_start                type2_end
    1    2019-04-01T00:35:12.061Z    2019-04-01T00:37:00.783Z
    2    2019-04-02T00:37:15.077Z    2019-04-02T00:39:01.393Z
    3    2019-04-03T00:39:18.268Z    2019-04-03T00:41:01.844Z
    4    2019-04-04T00:41:21.576Z    2019-04-04T00:43:02.071Z`

我一直在寻找最好的方法,并通读了Determine Whether Two Date Ranges Overlap并理解应该如何做,但是我对如何调用变量并使它们起作用还不甚了解。

#Here is what I have, but I am stuck and have no clue where to go form here:

import pandas as pd
from pandas import Timestamp
import numpy as np
from collections import namedtuple

colnames = ['type1', 'type1_start', 'type1_end', 'type2', 'type2_start', 'type2_end']
data = pd.read_csv('test.csv', names=colnames, parse_dates=['type1_start', 'type1_end','type2_start', 'type2_end'])

A_start = data['type1_start']
A_end = data['type1_end']
B_start= data['typer2_start']
B_end = data['type2_end']
t1 = data['type1']
t2 = data['type2']

r1 = (B_start, B_end)
r2 = (A_start, A_end)

def doesOverlap(r1, r2):
    if B_start > A_start:
        swap(r1, r2)
    if A_start > B_end:
        return false
    return true

最好有一个csv的结果是正确或错误的重叠。我也可以使用Efficiently find overlap of date-time ranges from 2 dataframes来运行数据,但结果不正确。我添加了几行我知道应该与数据重叠的行,但是行不起作用。我需要每种type2的开始/结束都要经历每种type1。

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

这是一种实现方法:

import pandas as pd


def overlaps(row):
    if ((row['type1_start'] < row['type2_start'] and row['type2_start'] < row['type1_end'])
            or (row['type1_start'] < row['type2_end'] and row['type2_end'] < row['type1_end'])):
        return True

    else:
        return False


colnames = ['type1', 'type1_start', 'type1_end', 'type2', 'type2_start', 'type2_end']
df = pd.read_csv('test.csv', names=colnames, parse_dates=[
    'type1_start', 'type1_end', 'type2_start', 'type2_end'])

df['overlap'] = df.apply(overlaps, axis=1)

print('\n', df)

给予:

    type1               type1_start                 type1_end  type2               type2_start                 type2_end  overlap
0  type1               type1_start                 type1_end  type2               type2_start                 type2_end    False
1      a  2019-03-01T00:43:18.046Z  2019-04-02T00:51:35.013Z      1  2019-04-01T00:35:12.061Z  2019-04-01T00:37:00.783Z     True
2      b  2019-04-01T02:16:46.490Z  2019-04-01T02:23:23.887Z      2  2019-04-02T00:37:15.077Z  2019-04-02T00:39:01.393Z    False
3      c  2019-04-01T03:49:31.981Z  2019-04-01T03:55:16.153Z      3  2019-04-03T00:39:18.268Z  2019-04-03T00:41:01.844Z    False
4      d  2019-04-01T05:21:22.131Z  2019-04-01T05:28:05.469Z      4  2019-04-04T00:41:21.576Z  2019-04-04T00:43:02.071Z    False

答案 1 :(得分:0)

df1下包含type1条记录,在df2下包含type2条记录:

df_new = df1.assign(key=1)\
            .merge(df2.assign(key=1), on='key')\
            .assign(has_overlap=lambda x: ~((x.type2_start > x.type1_end) | (x.type2_end < x.type1_start)))

REF:Performant cartesian product (CROSS JOIN) with pandas