NumPy - Data type Objects(dtype)
Last Updated :
23 Jan, 2025
Improve
Every ndarray has an associated data type (dtype) object. This data type object (dtype) informs us about the layout of the array. This means it gives us information about :
- Type of the data (integer, float, Python object etc.)
- Size of the data (number of bytes)
- Byte order of the data (little-endian or big-endian)
- If the data type is a sub-array, what is its shape and data type.
Table of Content
Creating Data Types Objects
A data type object in NumPy can be created in several ways:
Using Predefined Data Types
NumPy provides built-in data types like integers, floats, and strings.
import numpy as np
# Integer data type
x = np.array([1, 2, 3], dtype='int32')
print(x.dtype)
# Float data type
y = np.array([1.1, 2.2, 3.3], dtype='float64')
print(y.dtype)
Output
int32 float64
Using Custom Data Types
We can define a custom dtype using the numpy.dtype constructor.
import numpy as np
# Custom structured data type
x = np.dtype([('name', 'U10'), ('age', 'i4'), ('weight', 'f4')])
a = np.array([('Alice', 25, 55.5), ('Bob', 30, 72.3)], dtype=x)
print(a)
print(a.dtype)
Output
[('Alice', 25, 55.5) ('Bob', 30, 72.3)] [('name', '<U10'), ('age', '<i4'), ('weight', '<f4')]
Key Features of NumPy Data Type Objects
Byte Order : The byte order can be specified using prefixes:
- < for little-endian.
- > for big-endian.
import numpy as np
# Little-endian 4-byte integer
dtype_le = np.dtype('<i4')
# Big-endian 4-byte integer
dtype_be = np.dtype('>i4')
print(dtype_le)
print(dtype_be)
Output
int32 >i4
- Structured Data Types : NumPy supports structured or compound data types where multiple fields can have different data types. This is particularly useful for working with heterogeneous data.
import numpy as np
# Structured dtype with multiple fields
person_dtype = np.dtype([('name', 'S10'), ('age', 'i4'), ('height', 'f4')])
people = np.array([('John', 28, 5.9), ('Emma', 32, 5.5)], dtype=person_dtype)
print(people['name']) # Access 'name' field
Output
[b'John' b'Emma']
- Flexible String Types : Strings in NumPy can be defined with a specific maximum length:
import numpy as np
string_array = np.array(['apple', 'banana'], dtype='S6')
print(string_array)
Output
[b'apple' b'banana']
- Datetime Data Types: NumPy supports date and time data with the datetime64 and timedelta64 types
import numpy as np
a = np.array(['2025-01-23', '2025-01-24'], dtype='datetime64')
print(a)
Output
['2025-01-23' '2025-01-24']
Commonly Used NumPy Data Types
Data Type | Description | Example |
---|---|---|
int32 | 32-bit signed integer | -2,147,483,648 to 2,147,483,647 |
float64 | 64-bit floating-point number | 3.14, -1.0e6 |
complex128 | Complex number with float64 real and imaginary parts | 1+2j |
bool | Boolean values | True, False |
S or U | String data | 'hello' |
datetime64 | Date and time | '2025-01-01' |