Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Type Alias

np.dtype

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.

How it works

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.

Syntax

numpy.dtype(dtype, align=False, copy=False, [metadata])

Parameters

NameTypeDescription
dtypeobjectObject to be converted to a data type object.
alignbool, optionalAdds padding to structured fields to match a similar C-struct layout; also affects the aligned-structure flag.
copybool, optionalControls whether a new copy of the data-type object is made.
metadatadict, optionalOptional dictionary containing dtype metadata.

Returns

A NumPy data-type object describing the requested element representation.

Key points

  • np.dtype constructs and returns a NumPy data-type object.
  • An array's elements share one dtype, which describes how those elements are represented.
  • You can construct a dtype from combinations of NumPy's fundamental numeric types.
  • The optional metadata argument attaches a metadata dictionary to the dtype.
  • result_type is the related documented operation listed alongside np.dtype.

Common mistakes

  • With copy=False, the returned object may reference an existing built-in dtype rather than being a new object.
  • Use align=True when a structured dtype must follow C-like field padding and alignment.

Example: Create scalar and structured dtypes

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.

When to use it

Control array storage types

Specify the element representation when constructing or converting NumPy arrays.

Define structured records

Describe records with named fields and potentially nested or shaped members.

Analyze array types

Inspect or classify an array's element type using dtype properties and type predicates.

Interview questions

QWhat does 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.

QWhat kinds of input can 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)].

QWhat do the 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_type
  • numpy.issubdtype
  • numpy.ndarray.dtype
  • NumPy scalar types

Source: numpy.dtype. Checked 2026-10-15.