Return True if first argument is a typecode lower/equal in type hierarchy in Python

The numpy.issubdtype() method returns True if the first argument is a data type that is lower or equal in the NumPy type hierarchy compared to the second argument. This is useful for checking type compatibility and inheritance relationships in NumPy arrays.

Syntax

numpy.issubdtype(arg1, arg2)

Parameters

The method accepts two parameters ?

  • arg1 ? The data type or object coercible to a data type to be tested
  • arg2 ? The data type to compare against in the hierarchy

Understanding NumPy Type Hierarchy

NumPy has a type hierarchy where specific types inherit from more general types. For example, float64 is a subtype of floating, and int32 is a subtype of integer.

Example

Let's demonstrate how issubdtype() works with different data types ?

import numpy as np

print("Checking float type relationships:")
print("np.float64 subtype of np.float32:", np.issubdtype(np.float64, np.float32))
print("np.float64 subtype of np.floating:", np.issubdtype(np.float64, np.floating))
print("np.float32 subtype of np.floating:", np.issubdtype(np.float32, np.floating))

print("\nChecking integer type relationships:")
print("'i4' subtype of np.signedinteger:", np.issubdtype('i4', np.signedinteger))
print("'i8' subtype of np.signedinteger:", np.issubdtype('i8', np.signedinteger))
print("np.int32 subtype of np.integer:", np.issubdtype(np.int32, np.integer))
Checking float type relationships:
np.float64 subtype of np.float32: False
np.float64 subtype of np.floating: True
np.float32 subtype of np.floating: True

Checking integer type relationships:
'i4' subtype of np.signedinteger: True
'i8' subtype of np.signedinteger: True
np.int32 subtype of np.integer: True

Key Points

  • np.float64 is not a subtype of np.float32 because they are at the same level in the hierarchy
  • Both float64 and float32 are subtypes of the general floating type
  • String representations like 'i4' and 'i8' represent 32-bit and 64-bit integers respectively
  • All signed integer types inherit from signedinteger, which inherits from integer

Conclusion

The numpy.issubdtype() method is essential for type checking in NumPy operations. It helps determine if one data type can be safely used where another is expected based on the NumPy type hierarchy.

Updated on: 2026-03-26T19:20:48+05:30

160 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements