'is there any way to combine overlapping time period from datetime dataframes?

I have 2 dataframes each having two columns indicating the starting and ending of different events in a particular day. the condition is that there will an overlap in the time period across different data frames but no overlap of events will occur in the same data frame.

A new table must be created and if there is any overlap occurring between dataframeA and dataframeB, I need to combine the events such that only the earliest start and last end is present. Also the not overlapping time periods also needs to be present.

e.g.: Dataframe_A

Startime End Time
2021-06-20 12:04:48 2021-06-20 12:20:37
2021-06-20 13:04:49 2021-06-20 13:08:38
2021-06-20 13:10:10 2021-06-20 13:38:39
2021-06-20 14:10:34 2021-06-20 14:28:40
2021-06-20 15:06:25 2021-06-20 15:24:41

Dataframe_B

Startime End Time
2021-06-20 12:07:48 2021-06-20 12:27:37
2021-06-20 13:14:49 2021-06-20 13:38:38
2021-06-20 13:40:10 2021-06-20 13:58:39
2021-06-20 14:22:34 2021-06-20 14:47:40
2021-06-20 15:44:25 2021-06-20 15:54:41

Expected dataframe;

Column 1 Column 2
2021-06-20 12:04:48 2021-06-20 12:27:37
2021-06-20 13:04:49 2021-06-20 13:38:38
2021-06-20 13:40:10 2021-06-20 13:58:39
2021-06-20 14:10:34 2021-06-20 14:47:40
2021-06-20 15:06:25 2021-06-20 15:24:41
2021-06-20 15:44:25 2021-06-20 15:54:41


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source