4

How can I check that a numpy array if of float or complex dtype? For simple examples the following checks all work fine.

# these are True
a = np.zeros(10)
a.dtype == float
a.dtype == np.float
a.dtype == np.float64

b = np.zeros(10,dtype=complex)
b.dtype == complex
b.dtype == np.complex
b.dtype == np.complex128

However, I have an array of dtype dtype('>f8'). None of the previous comparisons identifies it as a float array. As far as I can see the endianness (> vs. <) is the issue there. Is there any general function to check whether the array is float or complex with all variations?

1
  • np.iscomplexobj tests the type of the dtype: issubclass(arr.dtype.type, np.complexfloating). The base dtypes have several levels of subclassing, so simple '==' tests don't always work. Commented Jul 8, 2019 at 16:25

2 Answers 2

9

Did you try numpy.isrealobj() and np.iscomplexobj()?

Your examples:

import numpy as np

a = np.zeros(10)
print(np.isrealobj(a)) # -> True
print(np.iscomplexobj(a)) # -> False

b = np.zeros(10,dtype=complex)
print(np.isrealobj(b)) # -> False
print(np.iscomplexobj(b)) # -> True

c=np.zeros(10, dtype='>f8')
print(np.isrealobj(c)) # -> True
print(np.iscomplexobj(c)) # -> False

The documentation states for np.isrealobj(x):

Return True if x is a not complex type or an array of complex numbers.

The type of the input is checked, not the value. So even if the input has an imaginary part equal to zero, isrealobj evaluates to False if the data type is complex.

There is also the possibility to check by value: np.isrealand np.iscomplex.

Does this help?

Sign up to request clarification or add additional context in comments.

2 Comments

Definitely helps, even for empty arrays, thanks for the suggestion.
If you are fully satisfied with my answer, please consider to accept it. Thanks!
1

You could use the all() function combined with the isinstance() function.

The all() function returns True if all the elements of a list is true, and you can check if every number in your numpy array is a float using isinstance(). Since isinstance() considers '>f8' as a float, the comparaisons doesn't raise the problem that you had.

Here is what it looks like :

b = np.zeros(10,dtype='>f8')
all(isinstance(x, float) for x in b)

Output :

True

5 Comments

Perhaps you should explain why the program should not test type(b[0]) == float or something like that. +1 anyway.
Is there also a solution for empty arrays, i.e. np.array([])? Just out of curiosity...
Using np.isrealobj() works also for empty arrays: np.isrealobj(np.array([])) is True and np.isrealobj(np.array([], dtype=complex)) is False.
@bproxauf all(b) returns true when true if b is empty there is no way around this just using the all function. But you could add a condition like so all(b) and len(b) != 0 to get the appropriate result
Checking the type of all elements of an array is not a good idea: Numpy guarantees that all elements have the same type (except for object arrays), and that is the main reason where the performance of numpy comes from.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.