从Unix UTC秒创建numpy datetime64

4 投票
2 回答
10937 浏览
提问于 2025-04-17 16:56

注意:我觉得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整数或浮点数转换成正确的dt64会很好。

https://stackoverflow.com/a/13704307/417578提到numpy 1.8.0可能可以满足我的需求,但在1.7.0中有没有什么方法可以实现呢?

2 个回答

2

也许我理解错了这个问题,但时区不就是一个显示的问题吗?

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

时间并没有以任何方式被“改变”:

some_time = datetime.datetime.utcfromtimestamp(1338624706)
dt64 = np.datetime64(1338624706,'s')
print dt64.astype(int64)
1338624706

这是针对numpy 1.7的内容。

4

这里有另一种在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

撰写回答