NumPy: Structured arrays#

Introduction#

Structured arrays are ndarrays whose datatype is a composition of simpler datatypes organized as a sequence of named fields.

>>> x = np.array([('Rex', 9, 81.0), ('Fido', 3, 27.0)],
...             dtype=[('name', 'U10'), ('age', 'i4'), ('weight', 'f4')])
>>> x
array([('Rex', 9, 81.), ('Fido', 3, 27.)],
      dtype=[('name', '<U10'), ('age', '<i4'), ('weight', '<f4')])

See how dtype is used here. The prototype of this dtype construction is [(field_name, field_type, field_shape), ...], field_shape is optional.

Here x is a 1-dimensional array with 2 rows, 3 columns, and the corresponding 3 fields for each column: 1. A string of length 10 or less named name, 2. a 32-bit integer named age, 3. a 32-bit floating-point number named weight

>>> x[1]
np.void(('Fido', 3, 27.0), dtype=[('name', '<U10'), ('age', '<i4'), ('weight', '<f4')])

You can access and modify individual fields/items by indexing the field name, like python dict

>>> x['age']
array([9, 3], dtype=int32)
>>> x['age'] = 5
>>> x
array([('Rex', 5, 81.), ('Fido', 5, 27.)],
      dtype=[('name', '<U10'), ('age', '<i4'), ('weight', '<f4')])
>>> x['age'][x['name'] == 'Rex'] = 9
>>> x
array([('Rex', 9, 81.), ('Fido', 5, 27.)],
      dtype=[('name', '<U10'), ('age', '<i4'), ('weight', '<f4')])