Control array storage types
Specify the element representation when constructing or converting NumPy arrays.
NumPy · Type Alias
np.dtype creates a NumPy data-type object from a type description. It describes array element representation and can also define structured fields, alignment, copying, and metadata.
Calling np.dtype converts a supported type description into a NumPy data-type object. The description can be a NumPy scalar type, a string, or a structured specification; optional alignment, copying, and metadata settings control construction details.
numpy.dtype(dtype, align=False, copy=False, [metadata])| Name | Type | Description |
|---|---|---|
dtype | object | Object to be converted to a data type object. |
align | bool, optional | Adds padding to structured fields to match a similar C-struct layout; also affects the aligned-structure flag. |
copy | bool, optional | Controls whether a new copy of the data-type object is made. |
metadata | dict, optional | Optional dictionary containing dtype metadata. |
A NumPy data-type object describing the requested element representation.
np.dtype constructs and returns a NumPy data-type object.metadata argument attaches a metadata dictionary to the dtype.result_type is the related documented operation listed alongside np.dtype.copy=False, the returned object may reference an existing built-in dtype rather than being a new object.align=True when a structured dtype must follow C-like field padding and alignment.import numpy as np
scalar = np.dtype(np.int16)
structured = np.dtype([("id", np.int32), ("score", np.float64)])
print(scalar)
print(structured.names)Creates a scalar dtype and a structured dtype, then displays the structured field names.
Specify the element representation when constructing or converting NumPy arrays.
Describe records with named fields and potentially nested or shaped members.
Inspect or classify an array's element type using dtype properties and type predicates.
np.dtype represent?np.dtype creates a data-type object describing the representation of array elements. NumPy arrays are homogeneous, so this object records characteristics such as the element type, width, and byte order.
Follow-up: How can a dtype be used for type classification? Use it with np.issubdtype, such as testing whether it is an integer or floating type.
np.dtype accept?The dtype argument is the description converted into a dtype object. It can be supplied using combinations of fundamental numeric types, including scalar types and structured field specifications.
Follow-up: How would you represent multiple named fields? Pass a sequence such as [('id', np.int32), ('score', np.float64)].
align and copy options do?align=True adds padding to structured fields so their layout matches what a C compiler would produce for a similar C struct. For structured dtypes, this also sets the sticky isalignedstruct flag.
Follow-up: What should be considered when using copy=False? The result can be a reference to a built-in dtype object instead of a newly copied object.
result_typenumpy.issubdtypenumpy.ndarray.dtypeNumPy scalar types