recarray
and structured
arrays are not designed to be convenient ways of naming columns. They are meant to hold a mix of data types, the kind of thing you might load from a CSV file - strings, integers, floats, dates.
The operations that can be performed across fields are limited. As you found, you cannot add a value to the whole array. You have to add it field by field - provided the field type is right. Similarly you can't sum the 2 fields, or take their mean (with np.sum
or np.mean
functions). Also can't reshape or transpose those arrays (exchanging fields for rows, etc).
Constructing np.array with overlapping fields in dtype is a current SO question that illustrates several ways of accessing a couple of fields as a 2 column array.
It is better to stick with normal nd arrays unless you really need the added flexibility of a structured array. If you want to access columns by name, consider defining variables, e.g. ind_x=0
, ind_y=1
, so you can use a[2:5, ind_x]
.