'how to find the consecutive value in time?

so basically I have a dataframe that looks like this

        df=pd.DataFrame([['PLACE A','4/10/2022 3:19:46PM','TYPE2','ALARM'],    
        ['PLACEB','4/10/2022 12:29:16AM','TYPE1','RESET'],
        ['PLACE A','4/10/2022 10:29:46PM', 'TYPE2','RESET'],
        ['PLACEB','4/10/2022 9:12:31AM','TYPE1','ALARM'],
        ['PLACEB','4/10/2022 11:26:16AM','TYPE1','RESET']])

OUTPUT

    0              1                   2          3
0   PLACE A  4/10/2022 3:19:46PM      TYPE2    ALARM
1   PLACE B  4/10/2022 12:29:16AM     TYPE1    RESET
2   PLACE A  4/10/2022 10:29:46PM     TYPE2    RESET
3   PLACE B  4/10/2022 9:12:31AM      TYPE1    ALARM
4   PLACE B  4/10/2022 11:26:16AM     TYPE1    RESET

I want to get a column with the duration of alarm. I tried splitting the df (one with alarm only and other with reset) and then merge) but there are two problems:

  1. if there multiple alarms for the same place and type then each alarm get multiple reset and could show negative duration !
  2. there could be reset (from the last day) and alarm only with no reset which would show duration of negative value.

so basically I want to merge if only the time of reset is after the alarm and it should be the first consecutive value for multiple alarms.



Sources

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

Source: Stack Overflow

Solution Source