How do I find out if a numpy array contains integers?

Question:

I know there is a simple solution to this but can’t seem to find it at the moment.

Given a numpy array, I need to know if the array contains integers.

Checking the dtype per-se is not enough, as there are multiple int dtypes (int8, int16, int32, int64 …).

Asked By: saffsd

||

Answers:

Found it in the numpy book! Page 23:

The other types in the hierarchy define particular categories of types.
These categories can be useful for testing whether or not the object
returned by self.dtype.type is of a particular class (using issubclass).

issubclass(n.dtype('int8').type, n.integer)
>>> True
issubclass(n.dtype('int16').type, n.integer)
>>> True
Answered By: saffsd

This also works:

  n.dtype('int8').kind == 'i'
Answered By: Dan

Checking for an integer type does not work for floats that are integers, e.g. 4. Better solution is np.equal(np.mod(x, 1), 0), as in:

>>> import numpy as np
>>> def isinteger(x):
...     return np.equal(np.mod(x, 1), 0)
... 
>>> foo = np.array([0., 1.5, 1.])
>>> bar = np.array([-5,  1,  2,  3, -4, -2,  0,  1,  0,  0, -1,  1])
>>> isinteger(foo)
array([ True, False,  True], dtype=bool)
>>> isinteger(bar)
array([ True,  True,  True,  True,  True,  True,  True,  True,  True,
    True,  True,  True], dtype=bool)
>>> isinteger(1.5)
False
>>> isinteger(1.)
True
>>> isinteger(1)
True
Answered By: Peter D

Numpy’s issubdtype() function can be used as follows:

import numpy as np

size=(3,3)
A = np.random.randint(0, 255, size)
B = np.random.random(size)

print 'Array A:n',  A
print 'Integers:', np.issubdtype(A[0,0], int)
print 'Floats:', np.issubdtype(A[0,0], float)

print 'nArray B:n',  B
print 'Integers:', np.issubdtype(B[0,0], int)
print 'Floats:', np.issubdtype(B[0,0], float)

Results:

Array A:
[[  9 224  33]
 [210 117  83]
 [206 139  60]]
Integers: True
Floats: False

Array B:
[[ 0.54221849  0.96021118  0.72322367]
 [ 0.02207826  0.55162813  0.52167972]
 [ 0.74106348  0.72457807  0.9705301 ]]
Integers: False
Floats: True

PS. Keep in mind that the elements of an array are always of the same datatype.

Answered By: funk

While the accepted answer from 2009 is still valid, there is a new and enhanced solution as of Numpy v0.19, released in September 2014:

All numerical numpy types are now registered with the type hierarchy
in the python numbers module.

This allows for checking the dtype against Python’s Numeric abstract base classes.

issubclass(np.dtype('int32').type, numbers.Integral)

You can test against numbers.Complex, numbers.Real and numbers.Integral.

Answered By: wedi

If you are looking to determine if a dtype is integral, then you look at the type hierarchy as the other answers suggest. However, if you want to check floats that may contain integers, you can use (x % 1) == 0, or the ufunc I recently wrote just for the purpose: https://github.com/madphysicist/is_integer_ufunc. After installing, you could run it with

from is_integer_ufunc import is_integer

is_integer(x)

It operates on integers and floating point types equally. The return value is a mask. For integer types, it is always True, while for floats it indicates the elements containing integer values.

Answered By: Mad Physicist
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.