Reputation: 616
What is the simplest way to identify whether a data point in numpy's array
is an integer? Currently I use numpy.dtype(x[i]).type
to return the type of the element i
of array x
and then
`if numpy.dtype(x[i]).type is numpy.int*`
to achieve this, where *
can be 8
, 32
or 64
. But it may also return uint
, thus this if
way can return False
. I wonder whether there exists a simple way to identify whether it is an integer, regardless of the exact int
type is. And how about float?
Upvotes: 1
Views: 905
Reputation: 13087
this might help (this effectively tests for a signed integer, in a similar fashion 'u' would be unsigned integer, etc.):
x[i].dtype.kind == 'i'
Upvotes: 3
Reputation: 97571
Use either:
issubclass(x[i].dtype.type, np.integer)
or
np.issubdtype(x[i].dtype, np.integer)
Your code of np.dtype(x)
is unlikely to do what you want - that doesn't get the dtype of x
, but tries to interpret x as a description of a new dtype. If you have a possibly non-numpy object you want a dtype of, you can use np.asanyarray(x).dtype
Upvotes: 1
Reputation: 462
You can use:
issubdtype(var, type)
Usage:
numpy.issubdtype(var_to_check, np.integer)
More information here How to determine if a number is any type of int (core or numpy, signed or not)?
Upvotes: 4