Reputation: 580
I am trying to create a new array with an existing array's elements. I keep getting:ValueError: Setting void-array with object members using buffer.
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
Upvotes: 1
Views: 2227
Reputation: 667
The problem is that matches
is not a list of tuples, so you cant make a structured array out of it. Instead it's a list of structured arrays, which need to be merged back into a single structured array. You can use numpy.lib.recfunctions.stack_arrays
for this:
In [21]: import numpy.lib.recfunctions as rfn
In [22]: rfn.stack_arrays(matches,usemask=False)
Out[22]:
array([(datetime.date(2013, 4, 5), 0, 1, 2, 3),
(datetime.date(2013, 4, 5), 5, 1, 5, 6)],
dtype=[('date', 'O'), ('open', 'i1'), ('high', 'i1'), ('low', 'i1'), ('close', 'i1')])
You could also consider doing away with the loop entirely:
In [23]: d[d['high'] == 1]
Out[23]:
array([(datetime.date(2013, 4, 5), 0, 1, 2, 3),
(datetime.date(2013, 4, 5), 5, 1, 5, 6)],
dtype=[('date', 'O'), ('open', 'i1'), ('high', 'i1'), ('low', 'i1'), ('close', 'i1')])
Which should be faster, to boot.
Upvotes: 4
Reputation: 26397
Change
rec = np.array(matches, dtype=stock_dt)
to
rec = np.array(matches)
When you're iterating over matches
you aren't dealing with tuples anymore so you shouldn't pass dtype=stock_dt
to np.array
again.
Upvotes: 1