注意:我认为 datetime64 正在做正确的事情。所以我会留下帖子,以防它有用。
从 numpy 1.7.0 开始,传入 np.datetime64 的秒数被解释为在本地时区中。有没有一种干净快速的方法可以将 unix utc 秒数导入 np.datetime64?我有 50M 的数组,似乎应该有办法告诉 np.datetime64 我的秒值是 UTC,不是吗?
datetime.datetime.utcfromtimestamp(1338624706)
datetime.datetime(2012, 6, 2, 8, 11, 46) # this is the time I'm looking for
np.datetime64(1338624706, 's')
numpy.datetime64('2012-06-02T01:11:46-0700') # Darn you ISO! Off by 7 hours
dt64 = np.datetime64(1338624706, 's')
dt64.astype(datetime.datetime)
datetime.datetime(2012, 6, 2, 8, 11, 46) # Wait, did it do the right thing?
# This seems like the best option at the moment,
# but requires building datetime.datetime objects:
dt64 = np.datetime64(datetime.datetime.utcfromtimestamp(1338624706))
numpy.datetime64('2012-06-02T01:11:46.000000-0700') # Show this
dt64.astype(datetime.datetime)
datetime.datetime(2012, 6, 2, 8, 11, 46) # Looks like it worked
我真的不想诉诸字符串操作。我很高兴能够将 unix utc ints 或浮点数组直接转换为正确的 dt64。
https://stackoverflow.com/a/13704307/417578暗示 numpy 1.8.0 可能会做我想做的事,但是有什么可以在 1.7.0 中使用吗?