How can one reliably determine if an object has a numpy type?
I realize that this question goes against the philosophy of duck typing, but idea is to make sure a fun
That actually depends on what you're looking for.
ndarray
, a isinstance(..., np.ndarray)
is probably the easiest. Make sure you don't reload numpy in the background as the module may be different, but otherwise, you should be OK. MaskedArrays
, matrix
, recarray
are all subclasses of ndarray
, so you should be set.shape
and a dtype
attribute. You can compare its dtype
to the basic dtypes, whose list you can find in np.core.numerictypes.genericTypeRank
. Note that the elements of this list are strings, so you'd have to do a tested.dtype is np.dtype(an_element_of_the_list)
...