Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

Numpy.isnan(): Tests NaN Values Element-wise

  • 25 Sep, 2025
  • Com 0
Numpy.isnan() Method in Python

Numpy.isnan() is a universal function (ufunc) that performs an element-wise test to check if values in an input array or scalar are NaN (Not a Number).

NaN is an exceptional floating-point value representing undefined or unrepresentable results, such as the outcome of operations like division by zero (e.g., 0/0) or (infinity – infinity).

Checking if a value is NaN in Numpy

import numpy as np

print(np.isnan(np.nan))

# Output: True  (It detects NaN correctly!)

print(np.isnan(5.0))

# Output: False (Finite number)

print(np.isnan(np.inf))

# Output: False

In this code, only np.nan returns the actual NaN value, so np.isnan() returned True for that value and False for all other values because they are not NaNs.

Syntax

numpy.isnan(x, 
            out=None, 
            where=True, 
            casting='same_kind', 
            order='K', 
            dtype=None, 
            subok=True[, signature])

Parameters

Argument Description
x (array_like, required) It represents input data to test. It can be a scalar value or an array.
out (ndarray or None, optional) It represents an existing array to store the result.
where (array_like, optional) It represents a boolean array or condition broadcastable to the input shape.
casting (str, optional) It controls type casting rules. Defaults to ‘same_kind’.
order (str, optional) It represents the memory layout for the output array.
dtype It represents the desired data type.
subok If it is True (default), subclasses of ndarray are allowed in the output.

Checking NaN values in a Numpy Array

If the input is a Numpy array, the output will be an array with True or False values. If the array element is NaN, it returns True; otherwise, it returns False.

Checking if an array contains an NaN value

import numpy as np

np_arr = np.array([1.0, np.nan, np.inf, 0.0])

print(np.isnan(np_arr))

# Output: [False  True False False]

Only the NaN element returns True.

Multi-Dimensional Array

Array dimensions don’t matter when it comes to this method. If the input array is 2D, the output array will also be 2D. But the array contains True or False values based on the result of the isnan() method.

Checking if a 2D array contains NaN in Numpy

import numpy as np

array_2d = np.array([[19.0, np.nan], [np.inf, 21.0]])

print(np.isnan(array_2d))

# Output:
# [[False  True]
#  [False False]]

Empty array

If the array is empty, the output will be empty too because there is nothing to check.

import numpy as np

empty_array = np.array([])

print(np.isnan(empty_array))

# Output:
# []

Using the “out” Parameter

If you want to store the result in another variable, you can use the “out” argument.

import numpy as np

arr = np.array([1.0, np.nan])

out_arr = np.zeros_like(arr, dtype=bool)

np.isnan(arr, out=out_arr)

print(out_arr)

# Output: [False  True]

Using the “where” argument

For efficient masked computations without touching all elements, you can use the “where” argument. It provides controls for which elements are calculated. It applies selectively.

import numpy as np

arr = np.array([1.0, np.nan])

where_cond = np.array([True, False])

out_arr = np.array([True, True], dtype=bool)

np.isnan(arr, out=out_arr, where=where_cond)

print(out_arr)

# Output: [False  True]

Counting NaN Values

For counting NaN values, you can use the np.sum() and np.isnan() methods.

import numpy as np

arr = np.array([11.0, np.nan, np.nan, 12.0])

print(np.sum(np.isnan(arr)))

# Output: 2

That’s all!

Post Views: 16
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

JavaScript Array forEach(): Looping Over an Array
Numpy.linalg.norm(): Find a Matrix or Vector Norm

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend