给定一列时间戳,有更好的方法来计算几个持续时间吗?

时间:2016-05-18 08:53:09

标签: python pandas

我只是想知道在给定使用pandas的时间戳列的情况下是否有更好的方法来聚合几个持续时间。我有一列时间戳看起来像这样:

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

并且我希望计算一个持续时间,假设两个时间戳之间的差异是> 10秒钟,给我这个:

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

目前,我正在遍历整个数据帧以获得上述结果。

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

0 个答案:

没有答案