I have a DataFrame which contains some room scheduling data.
Here is a sample of the data for the first few entries for Thursday and Friday morning:
DAYS BEGIN_TIME END_TIME
0 R 09:00 10:15
1 R 08:30 09:45
2 R 11:30 12:20
3 R 11:30 12:45
4 F 08:00 10:30
5 F 07:00 08:15
6 F 08:00 10:30
As a python defintion:
df = pd.DataFrame({'DAYS': {0: 'R', 1: 'R', 2: 'R', 3: 'R', 4: 'F', 5: 'F', 6: 'F'},
'BEGIN_TIME': {0: '09:00', 1: '08:30', 2: '11:30', 3: '11:30', 4: '08:00', 5: '07:00', 6: '08:00'},
'END_TIME': {0: '10:15', 1: '09:45', 2: '12:20', 3: '12:45', 4: '10:30', 5: '08:15', 6: '10:30'}}
)
R
represents Thursday and F
represents Friday. There are also M
, T
, and W
in this column.
BEGIN_TIME
and END_TIME
represent the start and end time for someone to be using the room, in hours and minutes, in 24-hour notation, HH:MM.
I would like to determine on which days, and times the room has collisions (multiple people trying to use the room at the same time).
For the sample data, I'd like to receive something like:
DAYS BEGIN_TIME END_TIME USERS
0 R 08:30 9:00 1
1 R 09:00 9:45 2
2 R 09:45 10:15 1
3 R 11:30 12:20 2
4 R 12:20 12:45 1
5 F 07:00 8:00 1
6 F 08:00 08:15 3
7 F 08:15 10:30 2
So far, in my research I found this answer to Count overlapping time frames in a pandas dataframe, grouped by person.
import pandas as pd
df = pd.DataFrame({'DAYS': {0: 'R', 1: 'R', 2: 'R', 3: 'R', 4: 'F', 5: 'F', 6: 'F'},
'BEGIN_TIME': {0: '09:00', 1: '08:30', 2: '11:30', 3: '11:30', 4: '08:00', 5: '07:00', 6: '08:00'},
'END_TIME': {0: '10:15', 1: '09:45', 2: '12:20', 3: '12:45', 4: '10:30', 5: '08:15', 6: '10:30'}}
)
# Convert to DateTime
df["BEGIN_TIME"] = df["BEGIN_TIME"].astype("datetime64[ns]")
df["END_TIME"] = df["END_TIME"].astype("datetime64[ns]")
# Code from linked SO Answer
ends = df['BEGIN_TIME'].values < df['END_TIME'].values[:, None]
starts = df['BEGIN_TIME'].values > df['BEGIN_TIME'].values[:, None]
same_group = (df['DAYS'].values == df['DAYS'].values[:, None])
df['OVERLAP'] = (ends & starts & same_group).sum(1)
print(df)
And while this does tell me about certain collisions, it doesn't help when trying to find specifically what times have conflict.
I also looked through Pandas: Count time interval intersections over a group by but the answers here also just looked at counting overlaps, not breaking out ranges into specific overlapping times.
I don't know where to go from here, can someone point me in the right direction?