为什么numpy数组不能隐式地从datetime转换为np.datetime64? [英] Why cannot numpy arrays convert from datetime to np.datetime64 implicitly?

查看:924
本文介绍了为什么numpy数组不能隐式地从datetime转换为np.datetime64?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说,我有一个 datetime

given_time = datetime(2013, 10, 8, 0, 0, 33, 945109,
                      tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=60, 
                                                             name=None))

我想将其转换为 np.datetime64

np.datetime64(given_time)
> numpy.datetime64('2013-10-08T00:00:33.945109+0100')

。但是,如果我有一个数组 given_time

It works well. However, if I have an array of given_time:

given_times = np.array([given_time]*3) # dtype is object

两个 given_times .stype('datetime64') given_times = np.array([given_time] * 3,dtype = np.datetime64)将触发 TypeError:根据规则'same_kind'不能将datetime.datetime对象从元数据[us]转换为[D]

所以,我必须指定单位:

So, I have to specify the unit:

given_times.astype('datetime64[us]')
# or
given_times = np.array([given_time]*3, dtype='datetime64[us]')


$ b $我的问题是,为什么我必须在这里指定单位?它不需要 np.datatime64 构造函数中的单元。

推荐答案

我知道这是一个古老的问题,但如果有人遇到这个问题,我会尝试回答。

I know it's an old question, but I'd try to answer in case anyone else comes across this.


  1. 截至1.11, numpy 不尝试自动将date / datetime对象的迭代转换为datetime64数组,这从此摘录

  1. As of 1.11, numpy doesn't try to automatically convert iterables of date/datetime objects to datetime64 arrays, this is pretty clear from this excerpt in the test suite:



# at the moment, we don't automatically convert these to datetime64

dt = datetime.date(1970, 1, 1)
arr = np.array([dt])
assert_equal(arr.dtype, np.dtype('O'))

dt = datetime.datetime(1970, 1, 1, 12, 30, 40)     
arr = np.array([dt])
assert_equal(arr.dtype, np.dtype('O'))

理想情况下, numpy 将会使用正确单位的 datetime64 请参阅问题。

Ideally, numpy would figure that datetime64 with correct units could be used; see this issue.


  1. 从标量构建 datetime64 时,设置为 M8 [D] 对于日期对象,对于日期时间对象( M8 [us] blob / 1429c606643d1ad305e710c4a31cb6f398d04c53 / numpy / core / tests / test_datetime.py#L392rel =nofollow>相关测试)。

  1. When constructing datetime64 from a scalar, the unit it set to M8[D] for date objects and to M8[us] for datetime objects (a relevant test).

您指定 dtype ='datetime64',或者类似地, dtype ='M8',单位设置为通用,稍后解析为 M8 [D] (尽管将其解析为 M8 [D] ,请参见问题):

When you specify dtype='datetime64', or, similarly, dtype='M8', the units are set to "generic", which later resolves to M8[D] (although it would be logical to have it resolve to M8[D], see this issue):



>>> np.datetime_data(np.dtype('datetime64'))
('generic', 1)
>>> np.datetime_data(np.dtype('M8'))
('generic', 1)
>>> np.datetime_data(np.dtype('M8[D]'))
('D', 1)
>>> np.datetime_data(np.dtype('M8[us]'))
('us', 1)




  1. given_times.astype('datetime64')不再引发异常在1.11中修正了

  1. given_times.astype('datetime64') no longer raises an exception -- this was fixed in 1.11.

从1.11开始,datetime64对象是时区天真的,所以传递一个datetime对象与 tzinfo 设置像提供的示例将触发一个废弃警告。

Starting from 1.11, datetime64 objects are timezone-naive, so passing a datetime object with tzinfo set like in the provided example will trigger a deprecation warning.

这篇关于为什么numpy数组不能隐式地从datetime转换为np.datetime64?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆