root_numpy.rec2array¶
-
root_numpy.
rec2array
(rec, fields=None)¶ Convert a record/structured array into an ndarray with a homogeneous data type.
Parameters: rec : NumPy record/structured array
A NumPy structured array that will be cast into a homogenous data type.
fields : list of strings or string, optional (default=None)
The fields to include as columns in the output array. If None, then all columns will be included. All fields must have the same shape. See below regarding the case where
fields
is a string.Returns: array : NumPy ndarray
A new NumPy ndarray with homogeneous data types for all columns. If the fields are scalars the shape will be
(len(rec), num_fields)
. If the fields are arrays of lengthnum_things
the shape will be(len(rec), num_things, num_fields)
. Iffields
is a string (a single field), then the shape will be simplified to remove the last dimensionnum_fields
. This simplification will not occur iffields
is a list containing a single field.Examples
>>> from root_numpy import rec2array >>> import numpy as np >>> a = np.array([ ... (12345, 2., 2.1, True), ... (3, 4., 4.2, False),], ... dtype=[ ... ('x', np.int32), ... ('y', np.float32), ... ('z', np.float64), ... ('w', np.bool)]) >>> arr = rec2array(a) >>> arr array([[ 1.23450000e+04, 2.00000000e+00, 2.10000000e+00, 1.00000000e+00], [ 3.00000000e+00, 4.00000000e+00, 4.20000000e+00, 0.00000000e+00]]) >>> arr.dtype dtype('float64') >>> >>> a = np.array([ ... ([1, 2, 3], [4.5, 6, 9.5],), ... ([4, 5, 6], [3.3, 7.5, 8.4],),], ... dtype=[ ... ('x', np.int32, (3,)), ... ('y', np.float32, (3,))]) >>> arr = rec2array(a) >>> arr array([[[ 1. , 4.5 ], [ 2. , 6. ], [ 3. , 9.5 ]], [[ 4. , 3.29999995], [ 5. , 7.5 ], [ 6. , 8.39999962]]]) >>> arr.shape (2, 3, 2)