如何从 pandas.DatetimeIndex 转换为 numpy.datetime64?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13227865/
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 from pandas.DatetimeIndex to numpy.datetime64?
提问by Yariv
How to convert from pandas.DatetimeIndexto numpy.datetime64?
如何从 转换pandas.DatetimeIndex为numpy.datetime64?
I get:
我得到:
>>> type(df.index.to_datetime())
Out[56]: pandas.tseries.index.DatetimeIndex
Is it safe to do numpy.array(datetimeindex,dtype=numpy.datetime64)?
这样做安全numpy.array(datetimeindex,dtype=numpy.datetime64)吗?
回答by Wes McKinney
The data inside isof datetime64dtype (datetime64[ns]to be precise). Just take the valuesattribute of the index. Note it will be nanosecond unit.
里面的数据是的datetime64D型细胞(datetime64[ns]确切地说)。只需取values索引的属性即可。请注意,它将是纳秒单位。
回答by Calvin Cheng
I would do this:-
我会这样做:-
new_array = np.array(df.index.to_pydatetime(), dtype=numpy.datetime64)
using the to_pydatetime()method.
使用to_pydatetime()方法。

