假设:
ts = pd.to_datetime(['2014-01-08 08:00:42',
'2014-01-08 08:01:00',
'2014-01-08 08:01:12'])
有没有一种规定的方法可以得到每个元素之间的时间差(最好使用total_seconds
属性)的结果?
我现在的方法似乎过于冗长:
pd.Index((pd.Series(ts) - pd.Series(ts).shift())).total_seconds()
# Float64Index([nan, 18.0, 12.0], dtype='float64')
我并不关心最终的数据结构类型,无论它是列表、索引还是序列。
ts.to_series().diff().dt.total_seconds().values
? - Zero