numpy.asarray: how to check up that its result dtype is numeric?
You could check if the dtype of the array is a sub-dtype of np.number
. For example:
>>> np.issubdtype(np.complex128, np.number)True>>> np.issubdtype(np.int32, np.number)True>>> np.issubdtype(np.str_, np.number)False>>> np.issubdtype('O', np.number) # 'O' is objectFalse
Essentially, this just checks whether the dtype is below 'number' in the NumPy dtype hierarchy: