How do I find out if a numpy array contains integers?
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
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
This also works:
n.dtype('int8').kind == 'i'