Choose integer or floating-point precision and item size for an array.
Control numeric storage
NumPy · Class
numpy.dtype creates a data type object that describes how homogeneous ndarray elements are interpreted. It captures properties such as value kind, storage size, byte order, and structured layout.
A dtype object specifies how each fixed-size array item is interpreted, including its kind, byte size, byte order, and—when applicable—structured fields or sub-array shape. NumPy functions that accept a data type can receive a dtype object or a value convertible to one.
numpy.dtype(dtype, align=False, copy=False, metadata=None)| Name | Type | Description |
|---|---|---|
dtype | dtype or convertible specification | Data-type specification used to create the dtype object; it may be a dtype or something convertible to one. |
align | bool | Whether to align fields in a structured dtype according to C alignment rules. |
copy | bool | Whether the dtype's underlying data should be copied when constructing it. |
metadata | dict or None | Optional user metadata associated with the dtype. |
A NumPy data type object describing array elements.
import numpy as np
record_type = np.dtype([("count", np.int32), ("score", np.float64)])
values = np.array([(3, 9.5), (7, 8.25)], dtype=record_type)
print(values.dtype.itemsize)
print(values["score"])
Creates a structured dtype with two named fields, then uses it to interpret each array element and access the floating-point field.
Control numeric storage
Model records
Interpret binary data
Inspect arrays
A dtype describes how the bytes for one fixed-size array item should be interpreted. It captures the data kind, item size, byte order, and additional layout information for structured or sub-array types.
Follow-up: How would you represent a record containing an integer and a floating-point value? Use a structured dtype with named fields and their respective NumPy types.
A dtype can be created from combinations of fundamental numeric types, and it can also describe structured data. For structured data, its fields include names, field dtypes, and memory placement.
Follow-up: What extra information does a sub-array dtype carry? It specifies both the sub-array's shape and its data type.
NumPy arrays are homogeneous, so their elements are described by one dtype object. The dtype therefore determines how every element in the array is interpreted.
Follow-up: Why does item size matter? It determines how many bytes correspond to each array item in the fixed-size memory block.
numpy.ndarray.dtypenumpy.dtype.fieldsnumpy.dtype.namesnumpy.dtype.itemsizenumpy.dtype.subdtypenumpy.dtype.byteorder