Numpy isrealobj: How to Use np isrealobj() Method

Numpy isrealobj() function tests are element-wise, whether it is a real number (not infinity or not, Not a Number), and returns the result as a boolean array. The isrealobj() method takes an input array or any value as a parameter.

Numpy isrealobj()

The Numpy isrealobj() function checks if an array is of a complex type or if the array contains any complex number. If there is no complex number or the value of the imaginary part to the array elements is zero, it returns True otherwise, False.


numpy.isrealobj(input array/value)


The isrealobj() function takes one required parameter. The parameter is the input array or the value for which we have to check whether it is a real object or not.

Return Value

The isrealobj() function returns a Boolean value, which is True if the array or the variable doesn’t contain any imaginary value or it is not of a complex type; otherwise False.

Example programs on isrealobj()

Write the following inside the file.


import numpy as np

print("Real obj = ", np.isrealobj(2))
a = [1, 2, 3, 4]
print("Real obj a = ", np.isrealobj(a))

b = [[1j], [2], [3], [4]]
print("Real obj b = \n", np.isrealobj(b))


Real obj =  True
Real obj a =  [ True  True  True  True]
Real obj b =
 [ True]
 [ True]
 [ True]]

In this example, we have passed three types of objects in the isrealobj() function, and firstly we gave a scalar value of 2, which resulted in the real object.

In the second part, we passed an array containing an array of integer values.

Thirdly we passed a list that contained the imaginary values; hence it showed the output False.

Write a program to create an array using arange() function and then check if the elements of that array are real objects or not.

See the following code.


import numpy as np

a = np.arange(24).reshape(6, 4)

print(a, "\n")
print("Real object a: ", np.isrealobj(a), "\n")

b = np.arange(24).reshape(6, 4).dtype = complex
print(b, "\n")
print("Real object b: ", np.isrealobj(b))


[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]
 [16 17 18 19]
 [20 21 22 23]]

Real object a:  True

<class 'complex'>

Real object b:  True

In this, we have created an array of 24 elements and checked whether all the elements constitute a real object or not. In the second example, we changed the data type of the array to complex and then checked the value.


Python Numpy isrealobj() function returns True if x is not a complex type or an array of complex numbers. This is because the type of input is checked, not the value. So even if the input has the imaginary part equal to zero, the isrealobj() evaluates to False if the data type is complex.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.