如何从 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 20:28:45  来源:igfitidea点击:

How to convert from pandas.DatetimeIndex to numpy.datetime64?

pythondatetimenumpypandas

提问by Yariv

How to convert from pandas.DatetimeIndexto numpy.datetime64?

如何从 转换pandas.DatetimeIndexnumpy.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()方法。