pandas 从 numpy timedelta64 获取秒数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20738357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getting seconds from numpy timedelta64
提问by Fra
I have a datetime index in pandas
我在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?
有更好/更清洁的方法吗?
回答by alko
Your own answer is correct and good. Slightly different way is to specify scale constants with timedeltaexpression.
你自己的答案是正确和好的。稍微不同的方法是用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])

