确定一个日期/时间范围与第二个日期/时间范围如何重叠? [英] Determining how one date/time range overlaps with the second date/time range?
问题描述
我正在处理具有6列的test.csv
文件.我需要打开csv
并查看两个时间范围之间的重叠.
I am working out of a test.csv
file that has 6 columns. I need to open the csv
and see the over lap between two time ranges.
csv
的外观如下:
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
我使用的是先前对此进行评论的逻辑.但是我不能使csv
列起作用.我不断收到此错误ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
如果我实际上在变量中输入日期/时间,它将起作用!
I am using a logic that was commented on this earlier. But I can't make the csv
columns work. I keep getting this error ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
If I actually enter the date/time to the variables it works!
colnames = ['type1', 'type1_start', 'type1_end', 'type2',
'type2_start', 'type2_end']
data = pd.read_csv('test.csv', names=colnames)
A_start = data['type1_start']
A_end = data['type1_end']
B_start= data['type2_start']
B_end = data['type2_end']
type1 = data['type1']
type2 = data['type2']
if A_start < B_end and B_start < A_end:
print("{} and {} They overlap".format(type1, type2))
else:
print("{} and {} They do not overlap".format(type1, type2))
任何人都可以帮忙吗?
我的csv文件很长,有六列.而且我有更多的type2行而不是type1行.我需要检查是否有任何type2范围落在type1范围内.
My csv file is very long, and has six columns. And I have more of type2 rows than type1 rows. I need to check if any of the type2 range falls in the type1 range.
推荐答案
我认为您不需要为此导入任何模块.这些时间戳可以作为字符串(国际格式)进行比较.
I don't think you need to import any module for this. These time stamps can be compared as strings (international format).
确定两个范围A和B是否相交是一个简单的条件:
Determining if two ranges A and B intersect is a simple condition:
if A.start < B.end and B.start < A.end: # A and B have an overlap
这篇关于确定一个日期/时间范围与第二个日期/时间范围如何重叠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!