pandas
datetime
對象轉換爲np.datetime64
對象,其差異np.timedelta64
對象。
考慮這個
In [30]: df
Out[30]:
0 1
0 2014-02-28 13:30:19.926778 2014-02-28 13:30:47.178474
1 2014-02-28 13:30:29.814575 2014-02-28 13:30:51.183349
我可以
df[0] - df[1]
Out[31]:
0 -00:00:27.251696
1 -00:00:21.368774
dtype: timedelta64[ns]
考慮列式差異,因此我可以申請timedelta64
轉換。對於微秒
(df[0] - df[1]).apply(lambda x : x.astype('timedelta64[us]')) #no actual difference when displayed
或微秒爲整數
(df[0] - df[1]).apply(lambda x : x.astype('timedelta64[us]').astype('int'))
0 -27251696000
1 -21368774000
dtype: int64
編輯: 作爲suggessted由@Jeff,最後的表達式可以縮短
(df[0] - df[1]).astype('timedelta64[us]')
和
(df[0] - df[1]).astype('timedelta64[us]').astype('int')
大熊貓> = .13。
熊貓> = 0.13,你可以做''df [0] -df [1] .astype('timedelta [us]')'' – Jeff