如何转换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)…