Open In App

NumPy - Data type Objects(dtype)

Last Updated : 23 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

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.

Python
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.

Python
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.
Python
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.
Python
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:
Python
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
Python
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 TypeDescriptionExample
int3232-bit signed integer-2,147,483,648 to 2,147,483,647
float6464-bit floating-point number3.14, -1.0e6
complex128Complex number with float64 real and imaginary parts1+2j
boolBoolean valuesTrue, False
S or UString data'hello'
datetime64Date and time'2025-01-01'

Next Article
Article Tags :
Practice Tags :

Similar Reads