本文介绍了在datetime,timestamp和datetime64之间进行转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 numpy.datetime64 对象转换为 datetime.datetime (或 Timestamp )?



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

  import datetime 
import numpy as np
import pandas as pd
dt = datetime.datetime(2012,5,1)
#提取Timestamp对象的一种奇怪的方法,肯定有更好的方法?
ts = pd.DatetimeIndex([dt])[0]
dt64 = np.datetime64(dt)

在[7]中:dt
输出[7 ]:datetime.datetime(2012,5,1,0,0)

在[8]中:ts
输出[8]:< Timestamp:2012-05-01 00: 00:00>

在[9]中:dt64
输出[9]:numpy.datetime64('2012-05-01T01:00:00.000000 + 0100')

注意:从Timestamp获取日期时间很容易:

 在[10]中:ts.to_datetime()
Out [10]:datetime.datetime(2012,5,1,0,0)
但是我们如何提取 datetime 或$ $ c $从 numpy.datetime64 dt64 )中的时间戳





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

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

应该是 datetime.datetime(2002,6,28,1,0),以及不太长(!)( 1025222400000000000L )...

解决方案

numpy.datetime64 转换为代表UTC时间的日期时间对象 numpy -1.8

 >>> from datetime import datetime 
>>>>导入numpy为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,'')
>>> 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)
pre>

它在单个np.datetime64对象和nump.natdatetime64数组上工作。



想想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'#当前版本可通过pip安装numpy

I可以将 long 值重现为 numpy-1.8.0 ,安装为:

  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 ) / code>。



要获取datetime对象,您可以:

 >> > dt64.dtype 
dtype('< M8 [ns]')
>>> ns = 1e-9#纳秒中的秒数
>>>> 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)

表示datetime API是实验性的,可能会改变未来的numpy版本。


How do I convert a numpy.datetime64 object to a datetime.datetime (or Timestamp)?

In the following code, I create a datetime, timestamp and datetime64 objects.

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')

Note: it's easy to get the datetime from the Timestamp:

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

But how do we extract the datetime or Timestamp from a numpy.datetime64 (dt64)?

.

Update: a somewhat nasty example in my dataset (perhaps the motivating example) seems to be:

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

which should be datetime.datetime(2002, 6, 28, 1, 0), and not a long (!) (1025222400000000000L)...

解决方案

To convert numpy.datetime64 to datetime object that represents time in UTC on numpy-1.8:

>>> 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'

The above example assumes that a naive datetime object is interpreted by np.datetime64 as time in UTC.


To convert datetime to np.datetime64 and back (numpy-1.6):

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

It works both on a single np.datetime64 object and a numpy array of np.datetime64.

Think of np.datetime64 the same way you would about np.int8, np.int16, etc and apply the same methods to convert beetween Python objects such as int, datetime and corresponding numpy objects.

Your "nasty example" works correctly:

>>> 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

I can reproduce the long value on numpy-1.8.0 installed as:

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

The same example:

>>> 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'

It returns long because for numpy.datetime64 type .astype(datetime) is equivalent to .astype(object) that returns Python integer (long) on numpy-1.8.

To get datetime object you could:

>>> 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)

To get datetime64 that uses seconds directly:

>>> 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)

The numpy docs say that the datetime API is experimental and may change in future numpy versions.

这篇关于在datetime,timestamp和datetime64之间进行转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 10:29