Numpy.asarray: how to check if its dtype result is numeric?

I need to create numpy.ndarray from massive data using int, float or complex numbers.

I hope to do this with the numpy.asarray function.

I do not want to give it the strict dtype argument, because I want to convert complex values ​​to complex64 or complex128 , floats before float32 or float64 , etc.

But if I just ran numpy.ndarray(some_unknown_data) and looked at the dtype of my result, how can I understand that the data is numeric, not objects or strings or something else?

+7
python arrays types numpy
source share
1 answer

You can check if the dtype of the array is a subtype 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 object False 

Essentially, it just checks to see if the dtype is below 'number' in the dumpe hierarchy of NumPy :

enter image description here

+14
source share

All Articles