简体   繁体   English

从numpy timedelta64获得秒数

[英]getting seconds from numpy timedelta64

I have a datetime index in pandas 我在熊猫中有一个日期时间索引

index = np.array(['2013-11-11T12:36:00.078757888-0800',
                  '2013-11-11T12:36:03.692692992-0800',
                  '2013-11-11T12:36:07.085489920-0800',
                  '2013-11-11T12:36:08.957488128-0800'], dtype='datetime64[ns]')

I want to calculate the time difference in seconds. 我想以秒为单位计算时差。 The way I came up with is: 我想出的方式是:

diff(index).astype('float64')/1e9

is there a better/cleaner way? 有更好/更清洁的方式吗?

Your own answer is correct and good. 你自己的答案是正确和好的。 Slightly different way is to specify scale constants with timedelta expression. 稍有不同的方法是使用timedelta表达式指定比例常数。

For example, to scale to seconds: 例如,要缩放到秒:

>>> np.diff(index)/np.timedelta64(1, 's')
array([ 3.6139351 ,  3.39279693,  1.87199821])

To minutes: 分钟:

>>> np.diff(index)/np.timedelta64(1, 'm')
array([ 0.06023225,  0.05654662,  0.03119997])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM