Currently, I am pretty stuck on a starting point for a problem I am trying to figure out. I have a dataframe that contains 4 columns. I want to try and find the overlapping times by day and id. for example, my df looks like the following:
+------+--------------+-----------------------+----------------------+
| id | date | time_start | end_time |
+--------------------------------------------------------------------+
| 123 | 2019-11-10 | 2019-11-10 08:00:00 | 2019-11-10 08:30:00 |
| | | | |
| 123 | 2019-11-10 | 2019-11-10 08:15:00 | 2019-11-10 08:30:00 |
| | | | |
| 123 | 2019-11-10 | 2019-11-10 08:25:00 | 2019-11-10 08:45:00 |
| | | | |
| 123 | 2019-11-11 | 2019-11-11 08:00:00 | 2019-11-11 08:30:00 |
| | | | |
| 123 | 2019-11-11 | 2019-11-11 08:30:00 | 2019-11-11 09:00:00 |
+------+--------------+-----------------------+----------------------+
import pandas as pd
data = {'id':['123', '123', '123', '123','123'], 'date':['2019-11-10', '2019-11-10', '2019-11-10', '2019-11-11', '2019-11-11'],
'time_start':['2019-11-10 08:00:00', '2019-11-10 08:15:00', '2019-11-10 08:25:00', '2019-11-11 08:00:00', '2019-11-11 08:30:00'],
'end_time':['2019-11-10 08:30:00','2019-11-10 08:30:00','2019-11-10 08:45:00','2019-11-11 08:30:00','2019-11-11 09:00:00']}
df = pd.DataFrame(data)
,id,date,time_start,end_time
0,123,2019-11-10,2019-11-10 08:00:00,2019-11-10 08:30:00
1,123,2019-11-10,2019-11-10 08:15:00,2019-11-10 08:30:00
2,123,2019-11-10,2019-11-10 08:25:00,2019-11-10 08:45:00
3,123,2019-11-11,2019-11-11 08:00:00,2019-11-11 08:30:00
4,123,2019-11-11,2019-11-11 08:30:00,2019-11-11 09:00:00
I would like to see a result similar to the following:
+----+------------+----------------------+---------------------+---------------+-------------------------+-----------------+
|id | date | time_start | time_end | overlap_count | total_minutes_recorded | actual_minutes|
+--------------------------------------------------------------------------------------------------------------------------+
|123 | 2019-11-10 | 2019-11-10 08:00:00 | 2019-11-10 08:45:00 | 3 | 65 | 45 |
| | | | | | | |
|123 | 2019-11-11 | 2019-11-11 08:00:00 | 2019-11-11 09:00:00 | 0 | 60 | 60 |
+----+------------+----------------------+---------------------+---------------+-------------------------+-----------------+
I looked at other answers that start to give me insight on how to solve this problem, for example:
Pandas: Count time interval intersections over a group by
Most of these answers are just giving me a count of overlapping times, and it is taking a long awhile to compute. Are there any tips on how to start to solve this problem