I need an efficient heterogeneous array in which the first element is an int and the rest are floats. After creating it, however, basic array operations are exploding.
A = np.zeros(1, dtype='i4, f4, f4')
B = np.array([3,3,3])
A + B
TypeError: invalid type promotion
With a structured array like this, operations that call for iteration over the fields generally don't work.
Even adding
A
to itself does not work:In other words, there's a way of adding an
int
to anint
, afloat
to anint
, but not a way of adding aA
element to another element.An element of
A
is atuple
or anumpy.void
(depending on how you access it)To work across the fields of a structured array you usually have to iterate over the field names.
If all the fields have the same type, e.g.
dtype='i4, i4, i4'
, then it is possible to view the structured array as a homogeneous dtype, and perform regular math on it. But with your mix of floats and ints, that isn't possible.