减去Timestamps列给出了TypeError

时间:2015-09-24 20:34:48

标签: python pandas multidimensional-array dataframe series

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

                      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)

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

1 个答案:

答案 0 :(得分:5)

转换为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