【发布时间】:2019-07-25 18:36:47
【问题描述】:
我正在尝试将包含 numpy.datetime64 的 numpy 数组 new_feat_dt 转换为纪元时间。我想确保转换发生时日期保持 utc 格式? 我正在使用 numpy 1.16.4 和 python3.6 我尝试了两种转换方式,如下代码所示。
import numpy as np
new_feat_dt = [np.datetime64('2019-07-25T14:23:01'), np.datetime64('2019-07-25T14:25:01'), np.datetime64('2019-07-25T14:27:01')]
final= [(x - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1, 's') for x in new_feat_dt]
print (final)
print(type(final[0]))
final2= [np.datetime64(x,'s').astype(int) for x in new_feat_dt]
print (final2)
print(type(final2[0]))
以上代码的输出:
[1564064581.0, 1564064701.0, 1564064821.0]
<class 'numpy.float64'>
[1564064581, 1564064701, 1564064821]
<class 'numpy.int32'>
发生上述情况是因为 new_feat_dt 数组中的时间被视为 GMT。我希望它被视为我的当地时间,即('美国/东部')。 正确的转换应该是这样的: [1564078981,1564079101,1564079221]
【问题讨论】:
标签: numpy python-3.6 epoch