Understanding NumPy isnan() function (4 examples)

Updated: February 26, 2024 By: Guest Contributor Post a comment

Introduction

NumPy, standing for Numerical Python, is a fundamental package for numerical computations in Python. It introduces powerful data structures, notably arrays, that provide efficient storage and manipulation of data, facilitating high-performance computations. One of its array operations is isnan(), used to identify ‘NaN’ (Not a Number) values within an array. This tutorial guides you through the isnan() function with progressive examples.

What is isNaN() used for?

The numpy.isnan() function is used to check for NaN in an array. It returns a Boolean array of the same shape as the input, indicating whether each element is NaN or not. This is particularly useful in data cleaning, preprocessing, and analysis where NaN values might indicate missing or erroneous data.

Example 1: Basic Usage

Let’s start with a simple example to detect NaN values in a NumPy array.

import numpy as np

# Creating a NumPy array with NaN value
array = np.array([1, 2, np.nan, 4, 5])

# Using isnan()
result = np.isnan(array)

print(result)

Output:

[False, False, True, False, False]

In this example, np.isnan(array) returns a Boolean array, where True indicates the presence of a NaN value.

Example 2: Working with Multi-dimensional Arrays

NumPy’s versatility supports multi-dimensional arrays. Here’s how isnan() works with them.

import numpy as np

# Creating a 2D array
array_2d = np.array([[1, np.nan], [3, 4]])

# Applying isnan()
result = np.isnan(array_2d)

print(result)

Output:

[[False,  True]
 [False, False]]

This method remains effective regardless of the array’s dimensionality, making it incredibly versatile for data of any shape.

Example 3: Combining isnan() with Filtering

Now, let’s use isnan() in conjunction with array filtering to remove NaN values.

import numpy as np

# Creating an array with NaN values
array = np.array([1, np.nan, 2, np.nan, 3])

# Filtering out NaN values
filtered_array = array[~np.isnan(array)]

print(filtered_array)

Output:

[1. 2. 3.]

This example highlights the practical application of isnan() in data cleaning, allowing us to efficiently exclude NaN values from analysis or computations.

Example 4: Handling Complex Data Types

While NaN is typically associated with floating-point numbers, it’s worthwhile to explore its behavior with complex data types.

import numpy as np

# Creating a complex array
complex_array = np.array([1+1j, np.nan, 3+3j])

# Checking for NaN
result = np.isnan(complex_array)

print(result)

Output:

[False,  True, False]

This demonstration underlines that isnan() can indeed be applied to complex numbers, reinforcing its utility across a broader spectrum of data types.

Conclusion

The numpy.isnan() function is a powerful tool for identifying and handling NaN values within arrays, essential for data cleaning and preprocessing. Through these examples, we’ve seen its versatility and practical application in various scenarios, making it an invaluable asset in your NumPy toolkit.