pandas 来自 unix utc 秒的 numpy datetime64
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15053791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
numpy datetime64 from unix utc seconds
提问by Kurt Schwehr
Note: I think datetime64 is doing the right thing. So I'll just leave the post up in case it's useful.
注意:我认为 datetime64 正在做正确的事情。所以我会留下这个帖子以防万一它有用。
As of numpy 1.7.0, seconds passed in to a np.datetime64 are interpreted as being in a local timezone. Is there a clean and fast way to import a unix utc seconds to np.datetime64? I've got arrays with 50M of these and it seems that there should be a way to tell np.datetime64 that my seconds value is UTC, no?
从 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
I really do not want to resort to string operations. I would be nice to be able to convert an array of unix utc ints or floats straight to the correct dt64.
我真的不想诉诸字符串操作。如果能够将 unix utc 整数或浮点数组直接转换为正确的 dt64,我会很高兴。
https://stackoverflow.com/a/13704307/417578implies that numpy 1.8.0 might do what I want, but is there something that will work in 1.7.0?
https://stackoverflow.com/a/13704307/417578暗示 numpy 1.8.0 可能会做我想做的事,但是有什么东西可以在 1.7.0 中使用吗?
回答by Jeff
Heres another way in pandas (which handles the quirks in different versions of numpy datetime64 correctly, so this works in numpy 1.6.2) - I think u might need current master for this (0.11-dev)
这是 Pandas 中的另一种方式(它可以正确处理不同版本的 numpy datetime64 中的怪癖,所以这适用于 numpy 1.6.2) - 我认为你可能需要当前的主人(0.11-dev)
# obviously replace this by your utc seconds
# need to convert to the default in pandas of datetime64[ns]
z = pd.Series([(1338624706 + i)*1e9 for i in range(50)],dtype='datetime64[ns]')
In [35]: z.head()
Out[35]:
0 2012-06-02 08:11:46
1 2012-06-02 08:11:47
2 2012-06-02 08:11:48
3 2012-06-02 08:11:49
4 2012-06-02 08:11:50
Dtype: datetime64[ns]
# turn it into a DatetimeIndex and localize
lidx = pd.DatetimeIndex(z).tz_localize('UTC')
<class 'pandas.tseries.index.DatetimeIndex'>
[2012-06-02 08:11:46, ..., 2012-06-02 08:12:35]
Length: 50, Freq: None, Timezone: UTC
# now you have a nice object to say convert timezones
In [44]: lidx.tz_convert('US/Eastern')
Out[44]:
<class 'pandas.tseries.index.DatetimeIndex'>
[2012-06-02 04:11:46, ..., 2012-06-02 04:12:35]
Length: 50, Freq: None, Timezone: US/Eastern
回答by radikalus
Perhaps I misunderstand the question, but isn't the timezone just a display issue?
也许我误解了这个问题,但时区不只是一个显示问题吗?
utc_time = datetime.datetime.utcnow()
print utc_time
dt64 = np.datetime64(utc_time)
print dt64
print dt64.astype(datetime.datetime)
2013-02-24 17:30:53.586297
2013-02-24T11:30:53.586297-0600
2013-02-24 17:30:53.586297
The time hasn't been 'changed' in any way:
时间没有以任何方式“改变”:
some_time = datetime.datetime.utcfromtimestamp(1338624706)
dt64 = np.datetime64(1338624706,'s')
print dt64.astype(int64)
1338624706
This is as of numpy 1.7.
这是从 numpy 1.7 开始的。

