簡體   English   中英

numpy datetime64在RecArray中

[英]numpy datetime64 in recarray

我在創建具有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)

我收到以下錯誤(&輸出):

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

提示,有人嗎?

指定一個“日期” datetime 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'

numpy在日期時間上有一個頁面 ,它很繁重,但是可以回答大多數問題。

有兩件事要注意:

  • 就像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]。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM