Test whether similar int type of different sizes are subdtypes of integer class in Python

To test whether similar int type of different sizes are subdtypes of integer class, use the numpy.issubdtype() method in Python NumPy. The parameters are the dtype or object coercible to one.

Syntax

numpy.issubdtype(arg1, arg2)

Parameters:

  • arg1: dtype or object coercible to one
  • arg2: dtype or object coercible to one

Returns: Boolean value indicating whether arg1 is a subtype of arg2.

Testing Signed Integer Subtypes

First, let's check if different sized integer types are subtypes of np.signedinteger

import numpy as np

# Testing different signed integer sizes
print("Testing signed integer subtypes:")
print("int16 is subtype of signedinteger:", np.issubdtype(np.int16, np.signedinteger))
print("int32 is subtype of signedinteger:", np.issubdtype(np.int32, np.signedinteger))
print("int64 is subtype of signedinteger:", np.issubdtype(np.int64, np.signedinteger))
Testing signed integer subtypes:
int16 is subtype of signedinteger: True
int32 is subtype of signedinteger: True
int64 is subtype of signedinteger: True

Testing General Integer Subtypes

Now let's check if these types are subtypes of the more general np.integer class −

import numpy as np

# Testing against general integer class
print("Testing general integer subtypes:")
print("int16 is subtype of integer:", np.issubdtype(np.int16, np.integer))
print("int32 is subtype of integer:", np.issubdtype(np.int32, np.integer))
print("int64 is subtype of integer:", np.issubdtype(np.int64, np.integer))
Testing general integer subtypes:
int16 is subtype of integer: True
int32 is subtype of integer: True
int64 is subtype of integer: True

Complete Example

Here's a comprehensive example testing various integer types −

import numpy as np

print("NumPy Integer Subtype Testing\n")

# Different integer types to test
int_types = [np.int8, np.int16, np.int32, np.int64]
type_names = ["int8", "int16", "int32", "int64"]

print("Testing against np.signedinteger:")
for dtype, name in zip(int_types, type_names):
    result = np.issubdtype(dtype, np.signedinteger)
    print(f"{name:<6} is subtype of signedinteger: {result}")

print("\nTesting against np.integer:")
for dtype, name in zip(int_types, type_names):
    result = np.issubdtype(dtype, np.integer)
    print(f"{name:<6} is subtype of integer: {result}")
NumPy Integer Subtype Testing

Testing against np.signedinteger:
int8   is subtype of signedinteger: True
int16  is subtype of signedinteger: True
int32  is subtype of signedinteger: True
int64  is subtype of signedinteger: True

Testing against np.integer:
int8   is subtype of integer: True
int16  is subtype of integer: True
int32  is subtype of integer: True
int64  is subtype of integer: True

Key Points

  • All NumPy signed integer types (int8, int16, int32, int64) are subtypes of np.signedinteger
  • All NumPy signed integer types are also subtypes of the more general np.integer class
  • The issubdtype() method returns True when the first argument is a subtype of the second
  • This hierarchy allows for flexible type checking in NumPy operations

Conclusion

The numpy.issubdtype() method confirms that all NumPy integer types of different sizes (int16, int32, int64) are indeed subtypes of both np.signedinteger and np.integer classes. This hierarchical relationship is fundamental to NumPy's type system.

Updated on: 2026-03-26T19:11:05+05:30

133 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements