如何转换numpy。对象的Datetime64。datetime(或Timestamp)?

在下面的代码中,我创建了一个datetime、timestamp和datetime64对象。

import datetime
import numpy as np
import pandas as pd
dt = datetime.datetime(2012, 5, 1)
# A strange way to extract a Timestamp object, there's surely a better way?
ts = pd.DatetimeIndex([dt])[0]
dt64 = np.datetime64(dt)

In [7]: dt
Out[7]: datetime.datetime(2012, 5, 1, 0, 0)

In [8]: ts
Out[8]: <Timestamp: 2012-05-01 00:00:00>

In [9]: dt64
Out[9]: numpy.datetime64('2012-05-01T01:00:00.000000+0100')

注意:从Timestamp中很容易得到datetime:

In [10]: ts.to_datetime()
Out[10]: datetime.datetime(2012, 5, 1, 0, 0)

但是我们如何从numpy中提取datetime或Timestamp。datetime64 (dt64) ?

.

更新:在我的数据集中有一个有点讨厌的例子(也许是激励的例子)似乎是:

dt64 = numpy.datetime64('2002-06-28T01:00:00.000000000+0100')

它应该是datetime。datetime(2002,6,28,1,0),而不是long (!) (1025222400000000000L)…


当前回答

这篇文章已经写了4年了,我仍然在这个转换问题上挣扎——所以在某种意义上,这个问题在2017年仍然很活跃。numpy文档没有提供简单的转换算法,这让我有些震惊,但那是另一回事了。

I have come across another way to do the conversion that only involves modules numpy and datetime, it does not require pandas to be imported which seems to me to be a lot of code to import for such a simple conversion. I noticed that datetime64.astype(datetime.datetime) will return a datetime.datetime object if the original datetime64 is in micro-second units while other units return an integer timestamp. I use module xarray for data I/O from Netcdf files which uses the datetime64 in nanosecond units making the conversion fail unless you first convert to micro-second units. Here is the example conversion code,

import numpy as np
import datetime

def convert_datetime64_to_datetime( usert: np.datetime64 )->datetime.datetime:
    t = np.datetime64( usert, 'us').astype(datetime.datetime)
return t

它只在我的机器上测试过,我的机器是Python 3.6,最近发布了2017年Anaconda发行版。我只看了标量转换,没有检查基于数组的转换,尽管我猜它会很好。我也没有查看numpy datetime64源代码,看看这个操作是否有意义。

其他回答

import numpy as np
import pandas as pd 

def np64toDate(np64):
    return pd.to_datetime(str(np64)).replace(tzinfo=None).to_datetime()

使用此函数获取python的原生datetime对象

欢迎来到地狱。

你可以将datetime64对象传递给pandas。时间戳:

In [16]: Timestamp(numpy.datetime64('2012-05-01T01:00:00.000000'))
Out[16]: <Timestamp: 2012-05-01 01:00:00>

我注意到这在NumPy 1.6.1中并不能正常工作:

numpy.datetime64('2012-05-01T01:00:00.000000+0100')

同时,熊猫。To_datetime可以使用(这是开发版本之外的,还没有检查v0.9.1):

In [24]: pandas.to_datetime('2012-05-01T01:00:00.000000+0100')
Out[24]: datetime.datetime(2012, 5, 1, 1, 0, tzinfo=tzoffset(None, 3600))

转换numpy。datetime64 to datetime对象,在numpy-1.8上表示UTC时间:

>>> from datetime import datetime
>>> import numpy as np
>>> dt = datetime.utcnow()
>>> dt
datetime.datetime(2012, 12, 4, 19, 51, 25, 362455)
>>> dt64 = np.datetime64(dt)
>>> ts = (dt64 - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1, 's')
>>> ts
1354650685.3624549
>>> datetime.utcfromtimestamp(ts)
datetime.datetime(2012, 12, 4, 19, 51, 25, 362455)
>>> np.__version__
'1.8.0.dev-7b75899'

上面的例子假设天真的datetime对象由np解释。datetime64作为UTC时间。


将datetime转换为np。Datetime64和返回(numpy-1.6):

>>> np.datetime64(datetime.utcnow()).astype(datetime)
datetime.datetime(2012, 12, 4, 13, 34, 52, 827542)

它对单个np都有效。对象和np.datetime64的numpy数组。

想想np。datetime64与np.int8、np.int16等相同,并应用相同的方法在Python对象(如int、datetime和相应的numpy对象)之间进行转换。

你的“讨厌的例子”是正确的:

>>> from datetime import datetime
>>> import numpy 
>>> numpy.datetime64('2002-06-28T01:00:00.000000000+0100').astype(datetime)
datetime.datetime(2002, 6, 28, 0, 0)
>>> numpy.__version__
'1.6.2' # current version available via pip install numpy

我可以在安装的numpy-1.8.0上重新生成long值:

pip install git+https://github.com/numpy/numpy.git#egg=numpy-dev

同样的例子:

>>> from datetime import datetime
>>> import numpy
>>> numpy.datetime64('2002-06-28T01:00:00.000000000+0100').astype(datetime)
1025222400000000000L
>>> numpy.__version__
'1.8.0.dev-7b75899'

它返回long,因为对于numpy。datetime64类型.astype(datetime)等价于.astype(object),在numpy-1.8上返回Python整数(long)。

要获取datetime对象,可以:

>>> dt64.dtype
dtype('<M8[ns]')
>>> ns = 1e-9 # number of seconds in a nanosecond
>>> datetime.utcfromtimestamp(dt64.astype(int) * ns)
datetime.datetime(2002, 6, 28, 0, 0)

获取直接使用秒的datetime64:

>>> dt64 = numpy.datetime64('2002-06-28T01:00:00.000000000+0100', 's')
>>> dt64.dtype
dtype('<M8[s]')
>>> datetime.utcfromtimestamp(dt64.astype(int))
datetime.datetime(2002, 6, 28, 0, 0)

numpy文档说datetime API是实验性的,在未来的numpy版本中可能会改变。

你可以用pd。时间戳的构造函数。下面的图表可能对这个问题和相关问题有用。

这篇文章已经写了4年了,我仍然在这个转换问题上挣扎——所以在某种意义上,这个问题在2017年仍然很活跃。numpy文档没有提供简单的转换算法,这让我有些震惊,但那是另一回事了。

I have come across another way to do the conversion that only involves modules numpy and datetime, it does not require pandas to be imported which seems to me to be a lot of code to import for such a simple conversion. I noticed that datetime64.astype(datetime.datetime) will return a datetime.datetime object if the original datetime64 is in micro-second units while other units return an integer timestamp. I use module xarray for data I/O from Netcdf files which uses the datetime64 in nanosecond units making the conversion fail unless you first convert to micro-second units. Here is the example conversion code,

import numpy as np
import datetime

def convert_datetime64_to_datetime( usert: np.datetime64 )->datetime.datetime:
    t = np.datetime64( usert, 'us').astype(datetime.datetime)
return t

它只在我的机器上测试过,我的机器是Python 3.6,最近发布了2017年Anaconda发行版。我只看了标量转换,没有检查基于数组的转换,尽管我猜它会很好。我也没有查看numpy datetime64源代码,看看这个操作是否有意义。