How do I check if a numpy dtype is integral? I tried:
issubclass(np.int64, numbers.Integral)
but it gives False
.
Update: it now gives True
.
Numpy has a hierarchy of dtypes similar to a class hierarchy (the scalar types actually have a bona fide class hierarchy that mirrors the dtype hierarchy).
In Numpy>2.0, you should use np.isdtype
: np.isdtype(np.void, 'integral')
.
Otherwise, you can use np.issubdtype(some_dtype, np.integer)
to test if a dtype is an integer dtype. Note that like most dtype-consuming functions, np.issubdtype()
will convert its arguments to dtypes, so anything that can make a dtype via the np.dtype()
constructor can be used.
http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html#specifying-and-constructing-data-types
>>> import numpy as np
>>> np.issubdtype(np.int32, np.integer)
True
>>> np.issubdtype(np.float32, np.integer)
False
>>> np.issubdtype(np.complex64, np.integer)
False
>>> np.issubdtype(np.uint8, np.integer)
True
>>> np.issubdtype(np.bool, np.integer)
False
>>> np.issubdtype(np.void, np.integer)
False
Alternatively, the scalar types are now registered with the appropriate numbers
ABCs.