3

I have a df with two columns, timestamp & eventType.

timestamp is ordered in chronological order, and eventType can be either ['start', 'change', 'end', resolve].
['start', 'change'] denotes the start of an event 

['end','resolve'] denotes the end of an event


createdTime         actionName
2020-03-16 18:28:14 start
2020-03-17 19:12:42 end
2020-03-18 19:56:10 change
2020-03-19 21:29:13 change
2020-03-20 21:42:06 end
2020-03-21 18:28:14 start
2020-03-21 19:12:42 resolve
2020-03-22 19:56:10 change
2020-03-22 21:29:13 change
2020-03-23 21:42:06 end

I wish to calculate the time delta between the each start/change event to the next end/resolve event.

  • An event can have several start/change statuses before it is resolved, thus an event would need to take the initial start/change status as the 1st start/change event time.
  • The output would need to be a list of time deltas taken for each event in the df

Thanks in advance :)


Edit The expected outcome should be a list containing each time taken for each event.

event_times = ['24:44:28', '49:45.56', '0:44:28', '25:45:56']
e-o-rino
  • 31
  • 2

1 Answers1

0

Better late than never?

df['createdTime'] = pd.to_datetime(df.createdTime)

starts = ['start', 'change']
ends = ['end','resolve']

prev_status = 'end'
spans = []

for i in range(len(df)):
    curr_status = df.actionName[i]
    if curr_status in starts and prev_status in starts:
        pass
    elif curr_status in starts and prev_status in ends:
        start_time = df.createdTime[i]
    elif curr_status in ends and prev_status in starts:
        t = df.createdTime[i] - start_time
        hours = t.days * 24 + t.seconds // 3600
        minutes = t.seconds % 3600 // 60 
        seconds = t.seconds % 60
        spans.append(f"{hours}:{minutes}:{seconds}")
    elif curr_status in ends and prev_status in ends:
        raise ValueError (f"Two ends in a row at index {i}.")
    else:
        raise ValueError (f"Unrecognized action type at index {i}.")
    prev_status = curr_status

print(spans)

gives

['24:44:28', '49:45:56', '0:44:28', '25:45:56']
Frodnar
  • 2,129
  • 2
  • 6
  • 20