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

NumPy · Class

dtype

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.

How it works

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.

Syntax

numpy.dtype(dtype, align=False, copy=False, metadata=None)

Parameters

NameTypeDescription
dtypedtype or convertible specificationData-type specification used to create the dtype object; it may be a dtype or something convertible to one.
alignboolWhether to align fields in a structured dtype according to C alignment rules.
copyboolWhether the dtype's underlying data should be copied when constructing it.
metadatadict or NoneOptional user metadata associated with the dtype.

Returns

A NumPy data type object describing array elements.

Key points

  • dtype is the object that maps an array element's fixed-size bytes to a meaningful value.
  • It records the category of values, such as integers, floating-point numbers, or Python objects.
  • It defines the storage width of each value in bytes.
  • It can specify whether multibyte values use little-endian or big-endian byte order.
  • For sub-array elements, it records both the embedded array shape and the dtype used by that array.

Common mistakes

  • A dtype argument may be a dtype instance or a convertible specification; callers do not need to construct an instance in every case.

Example: Structured dtype

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.

When to use it

Choose integer or floating-point precision and item size for an array.

Control numeric storage

Define named fields with different dtypes in a structured array.

Model records

Specify byte order and layout when mapping fixed-size bytes to values.

Interpret binary data

Read an ndarray's dtype properties to understand its element representation.

Inspect arrays

Interview questions

QWhat does a NumPy dtype describe?

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.

QWhat information can a structured dtype contain?

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.

QWhy is dtype central to an ndarray?

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.dtype
  • numpy.dtype.fields
  • numpy.dtype.names
  • numpy.dtype.itemsize
  • numpy.dtype.subdtype
  • numpy.dtype.byteorder

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