43

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.

mbdevpl
  • 4,605
  • 4
  • 30
  • 42
Neil G
  • 32,138
  • 39
  • 156
  • 257

6 Answers6

71

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

In a future version of numpy, we will make sure that the scalar types are registered with the appropriate numbers ABCs.

Robert Kern
  • 13,118
  • 3
  • 35
  • 32
  • 5
    Just to follow up, [the upcoming numpy 1.9](https://github.com/numpy/numpy/pull/4547) will register the scalar types with the `numbers` ABCs so `issubclass(np.int32, numbers.Integral)` will work, thanks to [Martin Teichmann](https://github.com/tecki). – Robert Kern Mar 25 '14 at 19:01
  • `issubclass` can't take a dtype as its first argument though, as a dype is an instance of `np.dtype`. So, `np.issubdtype` might be the better option in that case – Zev Isert Jun 22 '22 at 00:45
  • I changed my mind, and accepted this answer because it fits better with the upcoming array API standard. – Neil G Mar 15 '23 at 00:30
18

Note that np.int64 is not a dtype, it's a Python type. If you have an actual dtype (accessed through the dtype field of an array), you can make use of the np.typecodes dict you discovered:

my_array.dtype.char in np.typecodes['AllInteger']

If you only have a type such as np.int64, you can first obtain a dtype that corresponds to the type, then query it as above:

>>> np.dtype(np.int64).char in np.typecodes['AllInteger']
True
user4815162342
  • 141,790
  • 18
  • 296
  • 355
8

Since this question was asked, NumPy has added the appropriate registration with numbers, so this works:

issubclass(np.int64, numbers.Integral)
issubclass(np.int64, numbers.Real)
issubclass(np.int64, numbers.Complex)

This is more elegant than diving down to a more esoteric NumPy interface.

To perform this check on a dtype instance, use its .type property:

issubclass(array.dtype.type, numbers.Integral)
issubclass(array.dtype.type, numbers.Real)
issubclass(array.dtype.type, numbers.Complex)
paulmelnikow
  • 16,895
  • 8
  • 63
  • 114
Neil G
  • 32,138
  • 39
  • 156
  • 257
7

Building off previous answers and comments, I have settled on using the type attribute of the dtype object with Python's builtin issubclass() method and the numbers module:

import numbers
import numpy

assert issubclass(numpy.dtype('int32').type, numbers.Integral)
assert not issubclass(numpy.dtype('float32').type, numbers.Integral)
John
  • 1,709
  • 1
  • 24
  • 27
0

Depending on the use case the ducktyping

import operator
int = operator.index(number)

is a good method in my opinion. Plus it needs nothing numpy specific.

The only disadvantage is that in some cases you would have to try/except it.

seberg
  • 8,785
  • 2
  • 31
  • 30
-1

Do you mean line 17?

In [13]:

import numpy as np
A=np.array([1,2,3])
In [14]:

A.dtype
Out[14]:
dtype('int32')
In [15]:

isinstance(A, np.ndarray) #A is not an instance of int32, it is an instance of ndarray
Out[15]:
True
In [16]:

A.dtype==np.int32 #but its dtype is int32
Out[16]:
True
In [17]:

issubclass(np.int32, int) #and int32 is a subclass of int
Out[17]:
True
CT Zhu
  • 52,648
  • 17
  • 120
  • 133