Development

Python math.isnan() Method

Captain Salem 2 min read

Python math.isnan() Method

The math.isnan() method in Python, provided by the math module, allows us to check if a given value is a NaN or Not A Number value or not. The function returns a Boolean True if the provided value is a NaN type, otherwise, it returns a BooleanFalse.

This is a very handy function especially when working with numerical data types to detect any missing or invalid values which are represented as NaN types.

In this tutorial, we will explore how to work with the isnan function in Python, including the function syntax, parameters, and return values. We will cover some basic examples of using this function.

Import the Math Module

Let’s start by importing the math module:

import math

Python isnan() Function Syntax

The following shows the syntax of the isnan() function in Python.

math.isnan(x)

Where x is the value to check. It could be a float or an integer.

Examples

Let’s take a look at some examples to understand how math.isnan() works.

Example 1 - Check if a value is NaN

import math

print(math.isnan(float('nan')))
print(math.isnan(10))

Output:

True
False

In the example above, the first print statement checks whether float('nan') is NaN or not, and since it is indeed NaN, it returns True.

The second print statement checks if 10 is NaN, and since it isn’t, it returns False.

Example 2 - Check NaN in a List

The math.isnan() method can be very useful when you need to check multiple values in a Python list.

import math

values = [10, float('nan'), 20, float('nan'), 30]

for value in values:
    if math.isnan(value):
        print(f"{value} is NaN")
    else:
        print(f"{value} is not NaN")

Output:

10 is not NaN
nan is NaN
20 is not NaN
nan is NaN
30 is not NaN

In this case, we use a for loop to iterate through a list that contains both numeric and NaN values.

We then use the math.isnan() function to check whether each value is NaN or not. and print the corresponding value.

Example 3 - Filter out NaN values from a List

We can also use math.isnan() to filter out the NaN values from a list.

import math

values = [10, float('nan'), 20, float('nan'), 30]

# Filter out the NaN values
filtered_values = [value for value in values if not math.isnan(value)]

print(filtered_values)

Output:

[10, 20, 30]

In the example above, we use Python list comprehension to create a new list that only contains the values that are not NaN.

Note: The math.isnan() will raise a TypeError if the argument is not numeric, so it’s generally a good idea to ensure the data being checked is indeed numeric.

Conclusion

In this tutorial, we learned how we can work with the isnan() function to check whether a given input is a NaN type or not. We also discussed various examples, including how to filter NaN or non-NaN values in a Python list.

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.