Check if a NumPy Array contains any NaN value

In this article, We will learn how to check if a NumPy Array contains any NaN value in Python.

Table of Contents

What is a NaN value?

The NaN stands for Not a Number, which is a numeric data type that can be interpreted as a value that is undefined or unrepresentable.
Usually NaN values are used to represent the missing data in a dataframe or a NumPy Array.

Given a NumPy array, we need to check if the NumPy Array contains any NaN value or not.

Example:             

	Given array = [1, 2, 3, 4, NaN, 5, 6]

	The Array has NaN value at 5th position.

There are multiple ways to check if a NumPy Array contains any NaN value. Let’s discuss all the methods one by one with a proper approach and a working code example.

Check if a NumPy Array contains any NaN value using isnan() method

Numpy module in python, provides a function numpy.isnan(), to check if an element is NaN or not. The isnan() method will take a array as an input and returns a boolean array of same size. The values in boolean array represent that if the element at that corresponding position in original array is a NaN or not. The value in boolean array is True where element is NaN, false otherwise.

Advertisements

Syntax of isnan()

numpy.isnan(arr)

Parameters:

arr          = The input array to be passed to the function.

Return:

Returns a boolean array, True where element is NaN, false otherwise.    

As this method returns a boolean array, we need to check if the array contain atleast one true value i.e, NaN value. The any() method can be used to find if there is at least one true value. The any() method returns True if any item in an array is true, otherwise it returns False.

Syntax of any()

any(arr)

Parameters:

arr = The input array to be passed to the function.

Return:

Returns a boolean value, True if any item in an array are true, otherwise it returns False.

Approach

  • Import numpy library and create a numpy array
  • Now pass the array to the isnan() method. It will return a boolean array. Where True value denotes the NaN values in original array.
  • Pass the boolean array to the any() method, and it will returns a boolean value
  • If the value is true print “Array has NaN values” else print “Array has no NaN values.”

Source code

import numpy as np

# Creating a NumPy array
arr = np.array([1, 2, 3, 4, np.NaN, 5, 6])


# Check if the NumPy array contains any NaN value
if(np.isnan(arr).any()):
    print("The Array contain NaN values")
else:
    print("The Array does not contain NaN values")

OUTPUT:

The Array contain NaN values

Check if a NumPy array contains any NaN value using isna() method

Pandas module in python, provides a function Pandas.isna(), to check if a element is NaN or not. The isna() method will take an array as an input and returns a boolean array. The values in boolean array represent that if the element at that corresponding position in original array is a NaN or not. The value in boolean array is True where element is NaN and false otherwise.

Syntax of isna()

pandas.isna(arr)

Parameters:

arr = The input array to be passed to the function.

Return:

Returns a boolean array. It will contain True where element is NaN in original array, false otherwise.    

As the method returns a boolean array, we need to check if the array contain at least one true value i.e, NaN value. The any() method can be used to find if there is at least one true value. The any(arr) accepts a numpy array as argument and method returns True if any item in an array is True, otherwise it returns False.

Approach

  • Import numpy library and create a numpy array
  • Now pass the array to the isna() method. It will return a boolean array. Where True value denotes the NaN values in original array.
  • Pass the boolean array to the any method, the any() will returns a boolean value
  • If the value is true print “Array has NaN values” else print “Array has no NaN values.”

Source code

import pandas as pd
import numpy as np

# Creating a NumPy array
arr = np.array([1, 2, 3, 4, np.NaN, 5, 6])

# Check if the NumPy array contains any NaN value
if(pd.isna(arr).any()):
    print("The Array contain NaN values")
else:
    print("The Array does not contain NaN values")

OUTPUT:

The Array contain NaN values

Check if a NumPy array has any NaN value using isnan() method of math module

Math module in python, provides a function math.isnan() to check if a element is NaN or not. The isnan() method will take a value as an input and returns a boolean value, The returned boolean value True if the element is NaN, false otherwise..

Now to check if there is any NaN in a NumPy Array, using a for loop, we will iterate over the array and apply isnan() method too each element in the array, if any element is NaN then break the loop and print the array has NaN values.

Approach

  • Import numpy library and create a numpy array
  • Initialise a boolean flag contain = False.
  • Iterate over the array using a loop and apply isnan() on each element. The isnan() will return a boolean value
  • If the returned value is true, set the boolean flag to True and print “Array has NaN values” and break the loop.
  • Outside the loop, check the contain flag, if it is False print “Array has no NaN values”

Source code

import numpy as np
import math

# Create a NumPy array
arr = np.array([1, 2, 3, 4, np.NaN, 5, 6])

# Check if the NumPy array contains any NaN value
contain=False
for i in arr:
    if(math.isnan(i)):
        contain = True
        break

if(contain):
    print("The Array contain NaN values")
else:
    print("The Array does not contain NaN values")

    

OUTPUT:

The Array contain NaN values

Check the equality of the value with itself to check if a NumPy array contains any NaN value.

When a value is checked for equality with itself, it will return True, but in the case of the NaN value, even when it are compared with itself, it will return False.

Now to check if there is a NaN in array, using a for loop we will iterate over the array and compare each element with itself using a equality operator. If any value is not equal with itself then it is a NaN value and print array has NaN values.

Approach

  • Import numpy library and create a numpy array
  • Initialize a boolean flag contain = False.
  • Iterate over the array using a loop and check for equality with itself
  • If the values are Not equal, set the boolean flag to True and print “Array has NaN values” and break the loop.
  • Outside the check the contain flag, if it is False print “Array has no NaN values”.

Source code

#importing the numpy library
import numpy as np

# creating  numpy array
arr = np.array([1, 2, 3, 4, np.NaN, 5, 6])


# Checking if the NumPy array contains any NaN value
contain= False
for i in arr:
    if ( i!=i ):
        contain = True
        break

if(contain):
    print("The Array contain NaN values")
else:
    print("The Array does not contain NaN values")


OUTPUT:

The Array contain NaN values

Summary

Great! you made it, We have discussed all possible methods to check if a NumPy array contains any NaN value or not. Happy learning.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published.

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

Scroll to Top