gpt4 book ai didi

python - 减去时间戳的列给我 TypeError

转载 作者:太空宇宙 更新时间:2023-11-04 08:53:31 26 4
gpt4 key购买 nike

我有一个看起来像这样的数据框

                      dt1               dt2
0 2011-09-12 09:57:03.839546 2011-09-12 09:51:29.332474
1 2011-09-12 09:57:03.841132 2011-09-12 09:55:29.332774
2 2011-09-12 10:57:28.581049 2011-09-12 10:55:29.332774
3 2011-09-12 10:57:28.581049 2011-09-12 10:55:29.332774
4 2011-09-12 10:57:28.582636 2011-09-12 10:55:29.332774

条目的类是pandas.tslib.Timestamp。当我做的时候

df['dt1'][:]-df['dt2'][:]

我很好,但是这个

df['dt1']-df['dt2']

失败并出现以下错误

TypeError: Argument 'values' has incorrect type (expected numpy.ndarray, got Series)

为什么会发生这种情况,我该如何解决?

最佳答案

转换为 datetime64 可以修复它。

df['dt1'] = pd.to_datetime(df['dt1'])
df['dt2'] = pd.to_datetime(df['dt2'])

df['dt1']-df['dt2']

0 00:05:34.507072
1 00:01:34.508358
2 00:01:59.248275
3 00:01:59.248275
4 00:01:59.249862

关于python - 减去时间戳的列给我 TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32770425/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com