dtype (NumPy)

A dtype describes the type and size of every element in a NumPy array — for example int64, float32, bool, or complex128. Because all elements share one dtype, NumPy can store and process them efficiently.

NumPy

The dtype is what lets NumPy pack data tightly and run fast C loops over it. It records both the kind (integer, float, boolean, complex, string) and the byte width, so int32 and int64 are different dtypes that use 4 and 8 bytes per element respectively.

NumPy infers a dtype when you build an array but you can force one with the dtype argument, and convert later with .astype(). Choosing a smaller type like float32 over float64 halves memory, which matters for large arrays, at the cost of precision.

Beware silent overflow: fixed-width integer dtypes wrap around rather than growing like Python ints, so an int8 holding 127 plus 1 becomes -128. Pick a width wide enough for your data's range.

Syntax

np.array([1, 2], dtype=np.float32)  |  a.astype('int32')

Examples

Python

import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a.dtype)

Output:

int64

Learn it in a lesson

See also

Back to the Dictionary