我試圖用現有數組的元素創建一個新數組。我不斷收到:ValueError:使用緩衝區設置對象成員的void-array。在嘗試創建數組時嘗試使用numpy保持ValueError
import numpy as np
import datetime
date = datetime.date(2013, 4, 5)
results = [(date,0,1,2,3), (date,5,1,5,6), (date,3,4,4,7)]
stock_dt = np.dtype([('date', object),
('open', np.int8),
('high', np.int8),
('low', np.int8),
('close', np.int8)])
d = np.array(results, dtype=stock_dt)
matches = []
for item in d:
if item['high'] == 1:
matches.append(item)
rec = np.array(matches, dtype=stock_dt)
print rec
如何使用第二個答案使用多個參數? – Wallace 2013-03-28 02:45:02
'd [d ['high'] == 1]'正在做的是使用'd ['high'] == 1'創建的布爾列表對數組'd'進行索引。要使用多個標準,可以使用'&'和'|'運算符,如'd [(d ['high'] == 1)&(d ['low'] == 2)]''。小心使這些過於複雜,因爲它可能很快就無法讀取。 – 2013-03-28 02:54:43
完美!謝謝你的幫助!! – Wallace 2013-03-28 02:59:26