0

I was just wondering if there was a better way to aggregate several durations given a column of timestamps using pandas. I have a column of timestamps that look like this:

        timestamp
-----------------
0       00:00:10
1       00:00:20
2       00:00:30
3       00:00:55
4       00:01:05

and I wish to calculate a duration given that the difference between two timestamps are > 10 seconds, giving me this:

timestamp | duration(seconds)
-----------------------------
00:00:10  | 20
00:00:55  | 10

Currently I am looping through the entire dataframe to arrive at the above result.

startTimeIndex = None
for row in df.itertuples():

    startTimeIndex = row[0] if startTimeIndex == None else startTimeIndex
    timestamp = row[2]

    if (row[0] + 1 < len(df.index)):
         if (df.at[row[0] + 1, 'date'] - timestamp).total_seconds() > 10:
            startTime = df.at[startTimeIndex,'date']
            time_dict[str(startTime)] = (timestamp - startTime).total_seconds()
            startTimeIndex = row[0] + 1
cookiedookie
  • 389
  • 1
  • 5
  • 17

0 Answers0