I would like to take the information from some fields and just write them into another variable using a list.
import numpy as np
var1 = np.array([(1,2,3,4),(11,22,33,44),(111,222,333,444)], dtype=([('field1', 'int32'),('field2','int32'),('field3','int32'),('field4','int32')]))
var2 = np.empty((1), dtype = ([('field1', 'int32'),('field2','int32'),('field5','int32'),('field6','int32')]))
myList = ['field1', 'field2']
I want to write the values from the 1st and 2nd fields and 1st row to var2. I try the following:
var2[(myList)] = var1[(myList)][0]
But I get the following error:
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
What I want to achieve the same if I perform:
var2['field1'] = var1['field1'][0]
var2['field2'] = var1['field2'][0]
How could I do this in order to able to perform this with higher lists avoiding a for loop over the list?
A list of fields works for fetching a subset of the fields
but not when used on the left side as a 'setter' (this may be an area of development).
So you need to iterate on the fields.
Iterating on field names is common practice in structured array code (as in
np.rec
functions). Typically a structured array will have many elements ('rows') but a few fields ('columns'), so iteration over fields is not expensive.In this case, all fields of
var2
are the sameint
dtype. So I can perform the assignment on the corresponding 2d viewvar2
data buffer is all ints, so it can be viewed either as fields or a regular array (2d or 1).var2.view(int)[:2] = var1[myList][0]
assignsvar1['field1'][0]
to both items ofvar2
. So I have to make it into a list or tuple.Alternatively I can view
var1
as well. With this I found I need toreshape
as well. Theview
produces a 1d array view of the buffer.Multifield assignment is under development, but I don't think it is in the official release yet. https://github.com/numpy/numpy/pull/6053
can also be expressed as:
(the compound view is more compact, though not any faster).