check type within numpy array

All entries in a numpy array are of the same type. The numpy type and the Python type are not the same thing. This can be a bit confusing, but the type numpy refers to is more like the types used by languages like C - you might say more low level closer the the machine.

You can not say which type is better, because it would be like comparing apple and oranges.


A slight variation from @rasen58 and @hpaulj:

To check if an np array, c, has elements of type float, c.dtype == np.floating works for me.


To directly answer the question, you can do this:

isinstance(arr.flat[0], np.floating)

  • .flat will collapse any number of dimensions down, so you can then access the 0th element easily.
  • np.floating will match any numpy float type

An array is an object of type np.ndarray. Its values or elements are stored in a data buffer, which can be thought of as a contiguous block of memory bytes. The bytes in the data buffer do not have a type, because they are not Python objects.

The array has a dtype parameter, which is used to interpret those bytes. If dtype is int32 (there are various synonyms), 4 bytes are interpreted as an integer. Accessing an element, say c[0] gives a new object that depends on the dtype, e.g. an object type np.int32.

c[0].item will give an Python object of the corresponding type:

In [2102]: c=np.array([1])
In [2103]: c.dtype
Out[2103]: dtype('int32')
In [2104]: type(c)
Out[2104]: numpy.ndarray
In [2105]: type(c[0])
Out[2105]: numpy.int32
In [2107]: c[0].item()
Out[2107]: 1
In [2108]: type(c[0].item())
Out[2108]: int

(And c[0].dtype is the same as for c.dtype; you don't need to index individual elements of an array to check their dtype).

The same 4 bytes of this array can be viewed as dtype int8 - a single byte integer.

In [2112]: c.view('b')
Out[2112]: array([1, 0, 0, 0], dtype=int8)

A single element of this alternate view is np.int8, but when I take item(), I get a Python integer. There isn't a int8 Python numeric type.

In [2113]: type(c.view('b')[0])
Out[2113]: numpy.int8
In [2115]: type(c.view('b')[0].item())
Out[2115]: int

A list contains pointers to Python objects, each of which has a type. So does an array of dtype=object. But the common numeric array does not contain Python integers or floats. It has a data buffer that can interpreted in various ways according to the dtype. Python integers don't come in different sizes, at least not to the same extent as numpy dtypes.

So the isinstance and type() stuff does not apply to the contents of an ndarray.

====================

From the comments I gather you are trying to convert integer arrays to float. You aren't converting scalars. If so then dtype is all that matters; an array always has a dtype. It's unclear whether you are ok with casting a np.float32 to np.float64.

I'd suggest studying, and experimenting with the np.can_cast function and the x.astype method.

x.astype(np.float64, copy=False)

for example will convert all int dtypes to float, without copying the ones that are already float64. It may copy and convert np.float32 ones.

Look also at the casting parameter of these functions.

===========================

I found in scipy.optimize.minimize another testing tool

In [156]: np.typecodes
Out[156]: 
{'All': '?bhilqpBHILQPefdgFDGSUVOMm',
 'AllFloat': 'efdgFDG',
 'AllInteger': 'bBhHiIlLqQpP',
 'Character': 'c',
 'Complex': 'FDG',
 'Datetime': 'Mm',
 'Float': 'efdg',
 'Integer': 'bhilqp',
 'UnsignedInteger': 'BHILQP'}

It can be used to check for integers with:

if x0.dtype.kind in np.typecodes["AllInteger"]:
    x0 = np.asarray(x0, dtype=float)