减去时间戳列会产生TypeError



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

                      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

相关内容

  • 没有找到相关文章

最新更新