BetterDocs
Home
Docs

Creation | np.rec.array()

Method:

np.rec.array(obj, dtype=None, shape=None, offset=0, strides=None, formats=None, names=None, titles=None, aligned=False, byteorder=None, copy=True)

Creates a record array that allows access to fields by attribute lookup as well as by index.

Returns:

np.recarray

Parameters:

obj: (array-like, str, int)-

The input data for creating the record array. It can be a structured array, a list of tuples, or a string representing the array.

import numpy as np

# Create a record array from a list of tuples
data = [(1, 'A', 2.5), (2, 'B', 3.7), (3, 'C', 1.9)]
r_array = np.rec.array(obj=data, dtype=[('id', np.float32), ('name', np.object_), ('value', np.float32)])
print(r_array)

# Output
# [(1., 'A', 2.5) (2., 'B', 3.7) (3., 'C', 1.9)]

print(r_array.id)  # Access 'id' field: [1. 2. 3.]
print(r_array.name)  # Access 'name' field: ['A' 'B' 'C']
print(r_array.value)  # Access 'value' field: [2.5 3.7 1.9]

The tuples being passed to data must have the same number of elements and ensure it can be converted into the specified dtype.

dtype: None, Optional-

Specifies the data type of the record array. It can be a list of tuples defining field names and types or any valid NumPy dtype.

import numpy as np

# Define dtype with fields
dtype = [('x', np.float32), ('y', np.int32), ('z', np.object_)]

# Create a record array
data = [(1, 2.5, 'foo'), (2, 3.5, 'bar')]
r_array = np.rec.array(data, dtype=dtype)
print(r_array)

"""
Output:
[(1., 2, 'foo') (2., 3, 'bar')]
"""
print(r_array.x)  # Output: [1. 2.]

Values: +

shape: None, Optional-

Specifies the shape of the record array.

import numpy as np

# Create record array with shape
data = [(1, 2.5, 'foo'), (2, 3.5, 'bar')]
r_array = np.rec.array(data, shape=(2))
print(r_array.shape)  # Output: (2,)

The shape must match the size of the input data if provided.

offset: int, Optional-

The number of bytes to skip at the start of the buffer when creating the array.

import numpy as np

# Metadata: 1 byte, followed by record (id: 1 byte, value: 2 bytes)
binary_data = bytes([0xFF, 0x01]) + np.int16(4660).tobytes()

# Define dtype using np.dtype
dtype = np.dtype([('id', np.int8), ('value', np.int16)])

# Create record array with offset to skip 1 byte of metadata
r_array = np.rec.array(binary_data, dtype=dtype, offset=1)

print(r_array)       # Output: [(1, 4660)]
print(r_array.id)    # Output: [1]
print(r_array.value) # Output: [4660]

strides: None, Optional-

In NumPy 2.1, the device parameter in np.rec.array() is still in an early stage as part of its updates to improve memory access and customization for record arrays. Previously, custom strides were not directly supported in np.rec.array() and had to be implemented through np.ndarray  .

formats: None, Optional-

It specifies the format for each of the fields.

import numpy as np

# Create a record array
r_array = np.rec.array([(1, 2.5), (3, 4.0)], formats="i4,f4")

print(r_array)

names: None, Optional-

A tuple of field names. It allows you to give names to the fields of the record array, making it easier to access them by name rather than by index.

import numpy as np
            
# Define names for fields
names = ('id', 'value', 'label')
# Create a record array with named fields
data = [(1, 2.5, 'A'), (2, 3.7, 'B')]
r_array = np.rec.array(data, names=names)
print(r_array)
            
"""
Output:
[(1, 2.5, 'A') (2, 3.7, 'B')]
"""
print(r_array.label)  # Output: ['A' 'B']

titles: None, Optional-

A tuple of titles for the fields. This provides an additional way to describe the fields, often used for documentation purposes, without changing their underlying names or types.

It is used only for easier way to describe the fields making it human friendly and has no additional use.

aligned: (True or False), Optional-

In NumPy, the aligned parameter in np.rec.array() is used to control whether the fields of a structured array (or record array) should be aligned to certain byte boundaries.

copy: (True or False), Optional-

It is similar to np.array(copy=)  .


Logo

BetterDocs

Support

EmailDiscordForms

Documentations

Python

Company

AboutDocs

Policies

Terms of ServicePrivacy Policy