Maria Wolfie
Maria Wolfie

Reputation: 11

Determining how one date/time range overlaps with the second date/time range?

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.

This is how csv looks like:

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

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!

working on getting time range intersection correctly

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))

Would anyone be able to help out?

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.

Upvotes: 0

Views: 84

Answers (1)

Alain T.
Alain T.

Reputation: 42143

I don't think you need to import any module for this. These time stamps can be compared as strings (international format).

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

Upvotes: 2

Related Questions