是否可以转换此类内容;
array([datetime.datetime(2014, 2, 1, 0, 0, 0, 100000),
datetime.datetime(2014, 2, 1, 0, 0, 0, 300000),
datetime.datetime(2014, 2, 1, 0, 0, 0, 500000), ...,
datetime.datetime(2014, 2, 1, 19, 30, 0, 500000),
datetime.datetime(2014, 2, 1, 19, 30, 0, 700000),
datetime.datetime(2014, 2, 1, 19, 30, 0, 900000)], dtype=object)
到此:
array([ 1.39277301e+09, 1.39277301e+09, 1.39277301e+09, ...,
1.39285442e+09, 1.39285442e+09, 1.39285442e+09])
我基本上想将datetime.datetime
转换为timestamp
,dtype = float
。
答案 0 :(得分:0)
这样的事情怎么样:
(Python 2.x):
"MMM DD, YYYY"
对于Python 3.x:
import datetime
temp_array = numpy.array([(i - datetime.datetime(1970, 1, 1)).total_seconds() for i in old_array])
new_array = temp_array.astype('float')
答案 1 :(得分:0)
获得public void ClearHistory() => FrameFacade.BackStack.Clear();
的最佳方法是将timestamp
中的纪元减去:
<强>代码:强>
datetime
<强>结果:强>
import datetime as dt
times = np.array([
dt.datetime(2014, 2, 1, 0, 0, 0, 100000),
dt.datetime(2014, 2, 1, 0, 0, 0, 300000),
dt.datetime(2014, 2, 1, 0, 0, 0, 500000),
])
# get a datetime that is equal to epoch
epoch = dt.datetime(1970, 1, 1)
for t in [(d - epoch).total_seconds() for d in times]:
print('%.6f' % t)
答案 2 :(得分:0)
mktime()
会转换为时间戳,但似乎会失去超过秒的准确度。
>>> import datetime
>>> from time import mktime
>>> x = datetime.datetime.now()
>>> y = mktime(x.timetuple())
>>> print(y)
1493096455.0
>>> type(y)
<type 'float'>
>>> z = y/1000000000
>>> print(z)
1.493096455
答案 3 :(得分:0)
In [475]: x
Out[475]:
array([datetime.datetime(2014, 2, 1, 0, 0, 0, 100000),
datetime.datetime(2014, 2, 1, 0, 0, 0, 300000),
datetime.datetime(2014, 2, 1, 0, 0, 0, 500000),
datetime.datetime(2014, 2, 1, 19, 30, 0, 500000),
datetime.datetime(2014, 2, 1, 19, 30, 0, 700000),
datetime.datetime(2014, 2, 1, 19, 30, 0, 900000)], dtype=object)
In [476]: x.astype('datetime64[us]')
Out[476]:
array(['2014-02-01T00:00:00.100000', '2014-02-01T00:00:00.300000',
'2014-02-01T00:00:00.500000', '2014-02-01T19:30:00.500000',
'2014-02-01T19:30:00.700000', '2014-02-01T19:30:00.900000'], dtype='datetime64[us]')
每个都保存为8字节的浮点数,被解释为日期时间。
In [477]: _.dtype
Out[477]: dtype('<M8[us]')
In [478]: __.itemsize
Out[478]: 8
您可以更改单位(年,日等);做数学等
In [479]: x1=x.astype('datetime64[us]')
In [480]: np.diff(x1)
Out[480]: array([ 200000, 200000, 70200000000, 200000, 200000], dtype='timedelta64[us]')