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.
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.]
np.int8: 8-bit signed integer (range: -128 to 127).
np.int16: 16-bit signed integer (range: -32,768 to 32,767).
np.int32: 32-bit signed integer (range: -2,147,483,648 to 2,147,483,647).
np.int64: 64-bit signed integer (large integer range).
np.uint8: 8-bit unsigned integer (range: 0 to 255).
np.uint16: 16-bit unsigned integer (range: 0 to 65,535).
np.uint32: 32-bit unsigned integer (range: 0 to 4,294,967,295).
np.uint64: 64-bit unsigned integer (large positive integer range).
np.float16: Half precision floating-point (16-bit, for low-precision computations).
np.float32: Single precision floating-point (32-bit).
np.float64: Double precision floating-point (64-bit, the default float in NumPy).
np.float128: Extended precision floating-point (128-bit, availability depends on system).
np.complex64: Complex number represented by two 32-bit floats (for real and imaginary parts).
np.complex128: Complex number represented by two 64-bit floats (default complex dtype).
np.complex256: Complex number represented by two 128-bit floats (system-dependent).
np.bool_: Boolean type, can be either True or False (stored as 1-bit but takes up a full byte).
np.str_: Fixed-length Unicode string, specified by S + length (e.g., S10 for a 10-character string).
np.unicode_: Fixed-length Unicode string with support for multiple characters (uses U).
np.object_: Allows storing any Python object, including mixed types, strings, or other arrays. Useful for heterogeneous data but slower than native NumPy types.
np.datetime64: Stores dates and times with varying precisions (e.g., Y, M, D, h, m, s, ms, us, ns, ps, fs, as). Example: np.datetime64('2003-10-02')
np.timedelta64: Represents time durations with units (same units as datetime64).
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]
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 .
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']
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.
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.
It is similar to np.array(copy=) .