Python 如何将 numpy datetime64 转换为 datetime

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29753060/
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-08-19 04:57:23  来源:igfitidea点击:

How to convert numpy datetime64 into datetime

pythondatetimenumpypandas

提问by user6396

I basically face the same problem posted here:Converting between datetime, Timestamp and datetime64

我基本上面临这里发布的相同问题:Converting between datetime, Timestamp and datetime64

but I couldn't find satisfying answer from it, my question how to extract datetime from numpy.datetime64 type:

但我无法从中找到令人满意的答案,我的问题是如何从 numpy.datetime64 类型中提取日期时间:

if I try:

如果我尝试:

np.datetime64('2012-06-18T02:00:05.453000000-0400').astype(datetime.datetime)

it gave me: 1339999205453000000L

它给了我:1339999205453000000L

my current solution is convert datetime64 into a string and then turn to datetime again. but it seems quite a silly method.

我目前的解决方案是将 datetime64 转换为字符串,然后再次转换为 datetime。但这似乎是一种很愚蠢的方法。

采纳答案by hpaulj

Borrowing from Converting between datetime, Timestamp and datetime64

借用 日期时间、时间戳和日期时间 64 之间的转换

In [220]: x
Out[220]: numpy.datetime64('2012-06-17T23:00:05.453000000-0700')

In [221]: datetime.datetime.utcfromtimestamp(x.tolist()/1e9)
Out[221]: datetime.datetime(2012, 6, 18, 6, 0, 5, 452999)

Accounting for timezones I think that's right. Looks rather clunky though.

考虑到时区,我认为这是正确的。不过看起来比较笨重。

astype('O') is better (I think) thantolist())`:

astype('O') is better (I think) thantolist())`:

In [294]: datetime.datetime.utcfromtimestamp(x.astype('O')/1e9)
Out[294]: datetime.datetime(2012, 6, 18, 6, 0, 5, 452999)

or to get datetime in local:

或在本地获取日期时间:

In [295]: datetime.datetime.fromtimestamp(x.astype('O')/1e9)

But in the test_datatime.pyfile https://github.com/numpy/numpy/blob/master/numpy/core/tests/test_datetime.py

但在test_datatime.py文件 https://github.com/numpy/numpy/blob/master/numpy/core/tests/test_datetime.py

I find some other options - first convert the general datetime64to one of the format that specifies units:

我找到了一些其他选项 - 首先将通用转换为datetime64指定单位的格式之一:

In [296]: x.astype('M8[D]').astype('O')
Out[296]: datetime.date(2012, 6, 18)

In [297]: x.astype('M8[ms]').astype('O')
Out[297]: datetime.datetime(2012, 6, 18, 6, 0, 5, 453000)

This works for arrays:

这适用于数组:

In [303]: np.array([[x,x],[x,x]],dtype='M8[ms]').astype('O')[0,1]
Out[303]: datetime.datetime(2012, 6, 18, 6, 0, 5, 453000)

回答by Jeff

Note that Timestamp IS a sub-class of datetime.datetime so the [4] will generally work

请注意,时间戳是 datetime.datetime 的子类,因此 [4] 通常会起作用

In [4]: pd.Timestamp(np.datetime64('2012-06-18T02:00:05.453000000-0400'))
Out[4]: Timestamp('2012-06-18 06:00:05.453000')

In [5]: pd.Timestamp(np.datetime64('2012-06-18T02:00:05.453000000-0400')).to_pydatetime()
Out[5]: datetime.datetime(2012, 6, 18, 6, 0, 5, 453000)