本文介绍了在recarray numpy的datetime64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法创建一个与datetime64类型记录阵列。我运行的Python 2.7,numpy的1.7。

I'm having trouble creating a record array with the datetime64 type. I'm running Python 2.7, Numpy 1.7.

下面是一个小例子:

p_dtype = np.dtype({"names": ['trns_id', 'trns_date', 'qty', 'price', 'amount', 'description', 'commission', 'fees'],
                    "formats": [long, "M8", float, float, float, "S40", float, float]})

p_row = (8609132959, np.datetime64('2012-05-01'), 337.574, 4.86, -1640.61, 'Bought 337.574 XYZ @ 4.86', 0.0, 0.0)

print p_list, p_dtype

p_array = np.array(p_row, dtype=p_dtype)

我得到以下错误(放大器,输出):

I get the following error (& output):

TypeError                                 Traceback (most recent call last)
<ipython-input-137-0b4de45b819c> in <module>()
      6 print p_list, p_dtype
      7 
----> 8 p_array = np.array(p_row, dtype=p_dtype)
      9 
     10 print "Array: %s, dtype: %s" % (p_array, p_array.dtype)

TypeError: Cannot cast NumPy timedelta64 scalar from metadata [D] to  according to the rule 'same_kind'

(8609132959.0, numpy.datetime64('2012-05-01'), 337.574, 4.86, -1640.61, 'Bought 337.574 PIMSX @ 4.86', 0.0, 0.0) [('trns_id', '<i8'), ('trns_date', '<M8'), ('qty', '<f8'), ('price', '<f8'), ('amount', '<f8'), ('description', 'S40'), ('commission', '<f8'), ('fees', '<f8')]

提示,任何人?

推荐答案

指定一个约会日期时间DTYPE。也就是说,M8 [D]。而不是M8'datetime64 [D]。而不是'datetime64

Specify a "date" datetime dtype. That is, "M8[D]" instead of "M8", or 'datetime64[D]' instead of 'datetime64'.

In [80]: np.array([(0,np.datetime64('2012-05-17'))],
   ....:          dtype=[('i',np.int),('date','datetime64[D]')])
Out[80]: 
array([(0, datetime.date(2012, 5, 17))], 
      dtype=[('i', '<i8'), ('date', '<M8[D]')])

请注意,您还可以在数据Feed中一样简单字符串(即'2012-05-17',而不是 np.datetime ('2012-05-17')对象)

Note you can also feed in your data as simply the string (ie '2012-05-17', instead of the np.datetime('2012-05-17') object)

In [81]: np.array([(0,'2012-05-17')],
   ....:          dtype=[('i',np.int),('date','datetime64[D]')])
Out[81]: 
array([(0, datetime.date(2012, 5, 17))], 
      dtype=[('i', '<i8'), ('date', '<M8[D]')])

看来,这些类型是间$ P $不同从结构DTYPE情况下,单一的DTYPE情况PTED。你不会碰到你是一个单一的DTYPE喜欢这里遇到的问题:

It seems that these types are interpreted differently in the single dtype case from the structured dtype case. You would not run into the problem you are having with a single dtype like here:

In [84]: np.array([np.datetime64('2012-05-17')], dtype='datetime64')   # no need for [D]
Out[84]: array(['2012-05-17'], dtype='datetime64[D]')

In [85]: np.array(['2012-05-17'], dtype='datetime64')   # no need for [D]
Out[85]: array(['2012-05-17'], dtype='datetime64[D]')

但要有条理和你有这个问题:

But make it structured and you do have the problem:

In [87]: np.array([(0,'2012-05-17')],
   ....:          dtype=[('i',np.int),('date','datetime64')])
---------------------------------------------------------------------------
ValueError: Cannot create a NumPy datetime other than NaT with generic units

In [88]: np.array([(0,np.datetime64('2012-05-17'))],
   ....:          dtype=[('i',np.int),('date','datetime64')])
---------------------------------------------------------------------------
TypeError: Cannot cast NumPy timedelta64 scalar from metadata [D] to  according to the rule 'same_kind'

这篇关于在recarray numpy的datetime64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 18:38