Construct an ndarray that allows field access using attributes.
Arrays may have a data-types containing fields, analagous to columns in a spread sheet. An example is [(x, int), (y, float)], where each entry in the array is a pair of (int, float). Normally, these attributes are accessed using dictionary lookups such as arr['x'] and arr['y']. Record arrays allow the fields to be accessed as members of the array, using arr.x and arr.y.
Parameters: | shape : tuple
dtype : data-type, optional
formats : list of data-types, optional
names : tuple of strings, optional
buf : buffer, optional
|
---|---|
Returns: | rec : recarray
|
See also
Notes
This constructor can be compared to empty: it creates a new record array but does not fill it with data. To create a reccord array from data, use one of the following methods:
Examples
Create an array with two fields, x and y:
>>> x = np.array([(1.0, 2), (3.0, 4)], dtype=[('x', float), ('y', int)])
>>> x
array([(1.0, 2), (3.0, 4)],
dtype=[('x', '<f8'), ('y', '<i4')])
>>> x['x']
array([ 1., 3.])
View the array as a record array:
>>> x = x.view(np.recarray)
>>> x.x
array([ 1., 3.])
>>> x.y
array([2, 4])
Create a new, empty record array:
>>> np.recarray((2,),
... dtype=[('x', int), ('y', float), ('z', int)]) #doctest: +SKIP
rec.array([(-1073741821, 1.2249118382103472e-301, 24547520),
(3471280, 1.2134086255804012e-316, 0)],
dtype=[('x', '<i4'), ('y', '<f8'), ('z', '<i4')])