dtype
The dtype attribute reports what kind of number each element is.
In this page:
Syntax
arr.dtype
arr.dtype.name
dtype
dtype is an object describing the element type, with properties like name, kind and itemsize. It is set on creation and changed with astype. Checking dtype early prevents many subtle bugs from integer division or overflow.
Note:
Print arr.dtype.name to get a clean string like int64.
Example: dtype
import numpy as np
a = np.array([1, 2, 3])
print(a.dtype, a.dtype.name)
a[0] = 9.9
print("after assigning 9.9:", a)
print(a.astype(np.float64).dtype)
# Output:
# int64 int64
# after assigning 9.9: [9 2 3]
# float64
Related Topics
Common Mistakes
- Assuming dtype changes automatically when values change
- Forgetting int arrays truncate assigned floats
- Confusing dtype with the Python type
Chapter Summary
- dtype describes element type
- Values assigned to an int array are truncated
- astype creates a converted copy
- dtype.name gives a readable label