Numpy奇怪的行为转换为datetime64 dtype [英] Numpy odd behaviour conversion to datetime64 dtype

查看:1275
本文介绍了Numpy奇怪的行为转换为datetime64 dtype的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import numpy
a = numpy.array([20090913, 20101020, 20110125])

你能解释为什么 numpy.datetime64(a.astype(S8)。tolist())正确转换但不是code> numpy.datetime64(a.astype(S8))?为什么numpy函数不接受numpy字符串和只有常规的python字符串?我正在使用Numpy 1.6.1与Python 2.7.2 Windows 7。

Can you explain why numpy.datetime64(a.astype("S8").tolist()) converts correctly but not numpy.datetime64(a.astype("S8"))? Why would a numpy function not accept numpy strings and only regular python strings? I am using Numpy 1.6.1 with Python 2.7.2 Windows 7.

推荐答案

复制结果:

>>> a = numpy.array([20090913, 20101020, 20110125])

>>> numpy.datetime64(a.astype("S8").tolist())
array([2009-09-13 00:00:00, 2010-10-20 00:00:00, 2011-01-25 00:00:00], dtype=datetime64[us])

>>> numpy.datetime64(a.astype("S8"))
array([1970-01-01 00:00:20.090913, 1970-01-01 00:00:20.101020,
       1970-01-01 00:00:20.110125], dtype=datetime64[us])

这是关键: p>

Here's the key:

>>> a.astype("S8").tolist()
['20090913', '20101020', '20110125']
>>> a.astype("S8")
array(['20090913', '20101020', '20110125'],
      dtype='|S8')

在第一种情况下,字符串参数传递到 numpy.datetime64 并正确解析,正如你所描述的那样。第二,它需要按照推测从 | S8 执行明确的胁迫。事实证明,这是被考虑,但目前显然不被支持

In the first case, the string arguments get passed on to numpy.datetime64 and get parsed properly, exactly as you've described. In the second, it needs to perform an explicit coercion from |S8 as surmised. It turns out this is being considered, but currently explicitly isn't supported:


这没有进去,因为
数组之后的datetime属性不存在您将它们转换为datetime64,因此可能会有一些
的直觉后果。当Martin实现
四元数dtype时,我们讨论了dtypes可以
公开显示在数组对象上的属性的可能性,如果这是
实现,我认为python $的转换和兼容性b $ b datetime和datetime64可能很自然。

This didn't go in, because the datetime properties don't exist on the arrays after you convert them to datetime64, so there could be some unintuitive consequences from that. When Martin implemented the quaternion dtype, we discussed the possibility that dtypes could expose properties that show up on the array object, and if this were implemented I think the conversion and compatibility between python datetime and datetime64 could be made quite natural.

文档有更多的您可能需要考虑的工作强制的示例,包括其他numpy时间格式。如果您觉得显式类型强制的需要是错误的,我建议您向numpy团队报告,如果可能,请提交自己的补丁。

The documentation has more examples of working coercions you may wish to consider, including from other numpy time formats. If you feel the need for explicit type coercion is in error, I'd recommend reporting it to the numpy team and, if possible, submitting your own patch.

这篇关于Numpy奇怪的行为转换为datetime64 dtype的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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