pandas 如何将 numpy.timedelta64 转换为分钟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26566107/
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
How to convert numpy.timedelta64 to minutes
提问by Behzad Shahbaz
I have a date time column in a Pandas DataFrame and I'd like to convert it to minutes or seconds.
我在 Pandas DataFrame 中有一个日期时间列,我想将其转换为分钟或秒。
For example: I want to convert 00:27:00to 27 mins.
例如:我想转换00:27:00为 27 分钟。
example = data['duration'][0]
example
result: numpy.timedelta64(1620000000000,'ns')
结果: numpy.timedelta64(1620000000000,'ns')
What's the best way to achieve this?
实现这一目标的最佳方法是什么?
回答by sebix
Use array.astype()to convert the type of an array safely:
用于array.astype()安全地转换数组的类型:
>>> import numpy as np
>>> a = np.timedelta64(1620000000000,'ns')
>>> a.astype('timedelta64[m]')
numpy.timedelta64(27,'m')

