Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Test whether similar data types of different sizes are not subdtypes of each other in Python
The numpy.issubdtype() method in Python NumPy tests whether one data type is a subtype of another. When checking similar data types of different sizes (like float32 vs float64), they are not considered subtypes of each other despite being related.
Syntax
numpy.issubdtype(arg1, arg2)
Parameters
arg1, arg2: Data types or objects coercible to data types to compare for subtype relationship.
Import Required Library
First, import the NumPy library ?
import numpy as np
Testing Float Data Types
Check whether different float sizes are subtypes of each other ?
import numpy as np
print("Checking float32 vs float64:")
print("float32 subtype of float64:", np.issubdtype(np.float32, np.float64))
print("float64 subtype of float32:", np.issubdtype(np.float64, np.float32))
Checking float32 vs float64: float32 subtype of float64: False float64 subtype of float32: False
Testing Integer Data Types
Check whether different integer sizes are subtypes of each other ?
import numpy as np
print("Checking integer data types:")
print("int16 subtype of int32:", np.issubdtype(np.int16, np.int32))
print("int32 subtype of int16:", np.issubdtype(np.int32, np.int16))
print("int64 subtype of int32:", np.issubdtype(np.int64, np.int32))
print("int32 subtype of int64:", np.issubdtype(np.int32, np.int64))
Checking integer data types: int16 subtype of int32: False int32 subtype of int16: False int64 subtype of int32: False int32 subtype of int64: False
Complete Example
import numpy as np
print("Testing NumPy data type relationships\n")
# Float data types
print("Float data type comparisons:")
print("float32 vs float64:", np.issubdtype(np.float32, np.float64))
print("float64 vs float32:", np.issubdtype(np.float64, np.float32))
print("\nInteger data type comparisons:")
print("int16 vs int32:", np.issubdtype(np.int16, np.int32))
print("int32 vs int16:", np.issubdtype(np.int32, np.int16))
print("int64 vs int32:", np.issubdtype(np.int64, np.int32))
print("int32 vs int64:", np.issubdtype(np.int32, np.int64))
# Show actual parent types
print("\nActual parent types:")
print("float32 parent:", np.issubdtype(np.float32, np.floating))
print("int32 parent:", np.issubdtype(np.int32, np.integer))
Testing NumPy data type relationships Float data type comparisons: float32 vs float64: False float64 vs float32: False Integer data type comparisons: int16 vs int32: False int32 vs int16: False int64 vs int32: False int32 vs int64: False Actual parent types: float32 parent: True int32 parent: True
Key Points
? Similar data types of different sizes (e.g., int16 and int32) are not subtypes of each other
? Each specific size represents a distinct data type in NumPy's type hierarchy
? All integer types are subtypes of np.integer, and all float types are subtypes of np.floating
Conclusion
NumPy treats data types of different sizes as separate, non-hierarchical types. Use issubdtype() with generic parent types like np.integer or np.floating to check broader type categories.
