How to identify numpy types in python?
Use the builtin type
function to get the type, then you can use the __module__
property to find out where it was defined:
>>> import numpy as npa = np.array([1, 2, 3])>>> type(a)<type 'numpy.ndarray'>>>> type(a).__module__'numpy'>>> type(a).__module__ == np.__name__True
The solution I've come up with is:
isinstance(y, (np.ndarray, np.generic) )
However, it's not 100% clear that all numpy types are guaranteed to be either np.ndarray
or np.generic
, and this probably isn't version robust.
Old question but I came up with a definitive answer with an example. Can't hurt to keep questions fresh as I had this same problem and didn't find a clear answer. The key is to make sure you have numpy
imported, and then run the isinstance
bool. While this may seem simple, if you are doing some computations across different data types, this small check can serve as a quick test before your start some numpy vectorized operation.
################### important part!##################import numpy as np##################### toy array for demo####################arr = np.asarray(range(1,100,2))######################### The instance check######################## isinstance(arr,np.ndarray)