NumPy data types ( called dtype ) define the type of elements stored in a NumPy array.
Unlike Python lists (which can hold mixed types), NumPy arrays are homogeneous meaning all elements have the same data type.
This design makes NumPy faster, more memory-efficient, and predictable.
What is dtype ?
dtype stands for data type.
It tells NumPy:
-
how much memory to allocate per element
-
how to interpret the stored bits
Example :
Python
import numpy as np arr = np.array([1, 2, 3]) print(arr.dtype) #output int64 # may be int32 on some systems Numeric Data Types :
| Type | Description |
|---|---|
int8, int16, int32, int64 | Signed integers |
uint8, uint16, uint32 | Unsigned integers |
float16, float32, float64 | Floating-point numbers |
complex64, complex128 | Complex numbers |
Non Numeric Data Types :
| Type | Description |
|---|---|
bool | Boolean (True / False) |
str_ | String |
object | Python objects (avoid if possible) |
Automatic Type Conversion :
NumPy automatically converts data to the most suitable common type.
Python
arr = np.array([1, 2.5, 3]) print(arr) print(arr.dtype) #output [1. 2.5 3. ] float64 Changing Data Type :
Convert an array to a different type using astype( ) .
Python
arr = np.array([1.2, 2.8, 3.6]) new_arr = arr.astype(int) print(new_arr) print(new_arr.dtype) #output [1 2 3] int64