在recarray中的numpy datetime64

时间:2013-05-17 21:55:36

标签: python arrays numpy

我在使用datetime64类型创建记录数组时遇到问题。我正在运行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)

我收到以下错误(& 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')]

提示,有人吗?

2 个答案:

答案 0 :(得分:3)

指定“日期”日期时间dtype。也就是说,"M8[D]"代替"M8",或'datetime64[D]'代替'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]')])

请注意,您也可以简单地将数据作为字符串(即'2012-05-17',而不是np.datetime('2012-05-17')对象)提供

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]')])

似乎这些类型在单个dtype情况下与结构化dtype情况的解释不同。你不会遇到像这样的单一dtype遇到的问题:

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]')

但要让它结构化,你确实遇到了问题:

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'

答案 1 :(得分:1)

numpy在日期时间有一个page,它相当重,但回答了大多数问题。

有两点需要注意:

  • 日期和时间之间的分离就像在python的datetime
  • 中一样
  • 特定于numpy([*]后缀)
  • 的使用环境

上面遇到的问题是第二种,

dtnow = datetime.datetime.now()
numpy.datetime64(dtnow, '[D]')
Traceback (most recent call last):
  File "", line 1, in 
TypeError: Cannot cast datetime.datetime object from metadata [us] to [D] according to the rule 'same_kind'
numpy.datetime64(dtnow, '[s]')

numpy.datetime64( '2015-06-27T14:53:21 + 0300')

如果您的日期时间永远不会超过datetime64 [D]就足够了。

但是,如果它有,我会建议使用datetime64 [s]二级上下文。