Skip to content
Advertisement

Finding whether there is any overlap between two date periods in DataFrame

I have the following pd.DataFrame

JavaScript

that looks like:

JavaScript

I want to create a new column received_medidation that states whether or not (boolean) the patient received medication between admission_timestamp and end_period (even if it was for only one second). So, the boolean should state if there is any time between admission_timestamp and end_period that overlaps with the time between start_med and end_med. The dtypes are all datetime64[ns].

I know that we can create boolean masks such as

JavaScript

… however I fail to understand how this could possibily solve the task above. Any help is appreciated.

Advertisement

Answer

If start_med is guaranteed to be later than admission_timestamp, then it suffices that start_med date is between admission_timestamp and end_period

JavaScript

If however, start_med can be before admission_timestamp, then that means 'start_med' < 'admission_timestamp' < 'end_med' also creates an intersection of dates. Then we include this case with the previous case using OR operator:

JavaScript

Note: The overall assumption here is that it’s always true that admission_timestamp < end_period and start_med < end_med , in which case the above logical expression catches all intersecting dates.

Output:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement