Pandas: 如何添加两个时间戳值?

Pandas: How can I add two timestamp values?

我正在尝试添加两个以上的时间戳值,我希望在 minutes/seconds 中看到输出。如何添加两个时间戳?我基本上想做:'1995-07-01 00:00:01' + '1995-07-01 00:05:06' 并查看是否 total time>=60minutes。 我试过这段代码:df['timestamp'][0]+df['timestamp'][1]。我提到了这个 post 但我的时间戳来自数据框。 我的数据框列的标题如下所示:

0   1995-07-01 00:00:01
1   1995-07-01 00:00:06
2   1995-07-01 00:00:09
3   1995-07-01 00:00:09
4   1995-07-01 00:00:09
Name: timestamp, dtype: datetime64[ns]

我收到此错误: TypeError: unsupported operand type(s) for +: 'Timestamp' and 'Timestamp'

#Adding two timestamps is not supported and not logical
#Probably, you really want to add the time rather than the timestamp itself
#This is how to extract the time from the timestamp then summing it up

import datetime
import time

t = ['1995-07-01 00:00:01','1995-07-01 00:00:06','1995-07-01 00:00:09','1995-07-01 00:00:09','1995-07-01 00:00:09']
tSum = datetime.timedelta()
df = pd.DataFrame(t, columns=['timestamp'])
for i in range(len(df)):
    df['timestamp'][i] = datetime.datetime.strptime(df['timestamp'][i], "%Y-%m-%d %H:%M:%S").time()
    dt=df['timestamp'][i]
    (hr, mi, sec) = (dt.hour, dt.minute, dt.second)
    sum = datetime.timedelta(hours=int(hr), minutes=int(mi),seconds=int(sec))
    tSum += sum
if tSum.seconds >= 60*60:
    print("more than 1 hour")
else:
    print("less than 1 hour")

问题是添加 Timestamps 没有意义。如果他们在不同的日子怎么办?你要的是 Timedelta 的总和。我们可以通过从整个系列中减去一个公共日期来创建 Timedeltas。让我们减去最小日期。然后总结Timedeltas。让 s 成为你的 Timestamps

系列
s.sub(s.dt.date.min()).sum().total_seconds()

34.0