Overlap in seconds between datetime range and a time range

Viewed 57

I have a dataframe like this:

df11 = pd.DataFrame(
    {
    "Start_date": ["2018-01-31 12:00:00", "2018-02-28 16:00:00", "2018-02-27 22:00:00"],
    "End_date": ["2019-01-31 21:45:00", "2019-03-24 22:00:00", "2018-02-28 01:00:00"],
    }
)
         Start_date          End_date
0   2018-01-31 12:00:00 2019-01-31 21:45:00
1   2018-02-28 16:00:00 2019-03-24 22:00:00
2   2018-02-27 22:00:00 2018-02-28 01:00:00

I need to check the overlap time duration in specific periods in seconds. My expected results are like this:

         Start_date          End_date        12h-16h  16h-22h  22h-00h  00h-02h30
0   2018-01-31 12:00:00 2019-01-31 21:45:00   14400    20700     0        0
1   2018-02-28 16:00:00 2019-03-24 22:00:00     0      21600     0        0
2   2018-02-27 22:00:00 2018-02-28 01:00:00     0        0      7200    3600

I know it`s completely wrong and I´ve tried other solutions. This is one of my attempts:

df11['12h-16h']=np.where(df11['Start_date']<timedelta(hours=16, minutes=0, seconds=0) & df11['End_date']>timedelta(hours=12, minutes=0, seconds=0),(np.minimum(df11['End_date'],timedelta(hours=16, minutes=0, seconds=0)))-(np.maximum(df11['Start_date'],timedelta(hours=12, minutes=0, seconds=0)))
0 Answers
Related