How do you check for nan and infinity in python?

How to check if a single value is NaN in python. There are approaches are using libraries [pandas, math and numpy] and without using libraries.

NaN stands for Not A Number and is one of the common ways to represent the missing value in the data. It is a special floating-point value and cannot be converted to any other type than float.

NaN value is one of the major problems in Data Analysis. It is very essential to deal with NaN in order to get the desired results.

Finding and dealing with NaN within an array, series or dataframe is easy. However, identifying a stand alone NaN value is tricky. In this article I explain five methods to deal with NaN in python. The first three methods involves in-built functions from libraries. The last two relies on properties of NaN for finding NaN values.

Method 1: Using Pandas Library

isna[] in pandas library can be used to check if the value is null/NaN. It will return True if the value is NaN/null.

import pandas as pd
x = float["nan"]
print[f"It's pd.isna : {pd.isna[x]}"]
OutputIt's pd.isna : True

Method 2: Using Numpy Library

isnan[] in numpy library can be used to check if the value is null/NaN. It is similar to isna[] in pandas.

import numpy as np
x = float["nan"]
print[f"It's np.isnan : {np.isnan[x]}"]
OutputIt's np.isnan : True

Method 3: Using math library

Math library provides has built-in mathematical functions. The library is applicable to all real numbers. cmath library can be used if dealing with complex numbers.
Math library has built in function isnan[] to check null/NaN values.

import math
x = float["nan"]
print[f"It's math.isnan : {math.isnan[x]}"]
OutputIt's math.isnan : True

Method 4: Comparing with itself

When I started my career working with big IT company, I had to undergo a training for the first month. The trainer, when introducing the concept of NaN values mentioned that they are like aliens we know nothing about. These aliens are constantly shapeshifting, and hence we cannot compare NaN value against itself.
The most common method to check for NaN values is to check if the variable is equal to itself. If it is not, then it must be NaN value.

def isNaN[num]:
return num!= num
x=float["nan"]
isNaN[x]
OutputTrue

Method 5: Checking the range

Another property of NaN which can be used to check for NaN is the range. All floating point values fall within the range of minus infinity to infinity.

infinity < any number< infinity

However, NaN values does not come within this range. Hence, NaN can be identified if the value does not fall within the range from minus infinity to infinity.

This can be implemented as below:

def isNaN[num]:
if float['-inf'] < float[num] < float['inf']:
return False
else:
return True
x=float["nan"]
isNaN[x]
OutputTrue

I hope you have found the above article helpful. I am sure there would be many other techniques to check for NaN values based on various other logics. Please share the other methods you have come across to check for NaN/ Null values.

Cheers!

Become a Member

I hope you like the article, I would highly recommend signing up for Medium Membership to read more articles by me or stories by thousands of other authors on variety of topics.
Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium.

Is it possible to set a number to NaN or infinity?

Yes, in fact there are several ways. A few work without any imports, while others require import, however for this answer I'll limit the libraries in the overview to standard-library and NumPy [which isn't standard-library but a very common third-party library].

The following table summarizes the ways how one can create a not-a-number or a positive or negative infinity float:

╒══════════╤══════════════╤════════════════════╤════════════════════╕
│   result │ NaN          │ Infinity           │ -Infinity          │
│ module   │              │                    │                    │
╞══════════╪══════════════╪════════════════════╪════════════════════╡
│ built-in │ float["nan"] │ float["inf"]       │ -float["inf"]      │
│          │              │ float["infinity"]  │ -float["infinity"] │
│          │              │ float["+inf"]      │ float["-inf"]      │
│          │              │ float["+infinity"] │ float["-infinity"] │
├──────────┼──────────────┼────────────────────┼────────────────────┤
│ math     │ math.nan     │ math.inf           │ -math.inf          │
├──────────┼──────────────┼────────────────────┼────────────────────┤
│ cmath    │ cmath.nan    │ cmath.inf          │ -cmath.inf         │
├──────────┼──────────────┼────────────────────┼────────────────────┤
│ numpy    │ numpy.nan    │ numpy.PINF         │ numpy.NINF         │
│          │ numpy.NaN    │ numpy.inf          │ -numpy.inf         │
│          │ numpy.NAN    │ numpy.infty        │ -numpy.infty       │
│          │              │ numpy.Inf          │ -numpy.Inf         │
│          │              │ numpy.Infinity     │ -numpy.Infinity    │
╘══════════╧══════════════╧════════════════════╧════════════════════╛

A couple remarks to the table:

  • The float constructor is actually case-insensitive, so you can also use float["NaN"] or float["InFiNiTy"].
  • The cmath and numpy constants return plain Python float objects.
  • The numpy.NINF is actually the only constant I know of that doesn't require the -.
  • It is possible to create complex NaN and Infinity with complex and cmath:

    ╒══════════╤════════════════╤═════════════════╤═════════════════════╤══════════════════════╕
    │   result │ NaN+0j         │ 0+NaNj          │ Inf+0j              │ 0+Infj               │
    │ module   │                │                 │                     │                      │
    ╞══════════╪════════════════╪═════════════════╪═════════════════════╪══════════════════════╡
    │ built-in │ complex["nan"] │ complex["nanj"] │ complex["inf"]      │ complex["infj"]      │
    │          │                │                 │ complex["infinity"] │ complex["infinityj"] │
    ├──────────┼────────────────┼─────────────────┼─────────────────────┼──────────────────────┤
    │ cmath    │ cmath.nan ¹    │ cmath.nanj      │ cmath.inf ¹         │ cmath.infj           │
    ╘══════════╧════════════════╧═════════════════╧═════════════════════╧══════════════════════╛
    

    The options with ¹ return a plain float, not a complex.

is there any function to check whether a number is infinity or not?

Yes there is - in fact there are several functions for NaN, Infinity, and neither Nan nor Inf. However these predefined functions are not built-in, they always require an import:

╒══════════╤═════════════╤════════════════╤════════════════════╕
│      for │ NaN         │ Infinity or    │ not NaN and        │
│          │             │ -Infinity      │ not Infinity and   │
│ module   │             │                │ not -Infinity      │
╞══════════╪═════════════╪════════════════╪════════════════════╡
│ math     │ math.isnan  │ math.isinf     │ math.isfinite      │
├──────────┼─────────────┼────────────────┼────────────────────┤
│ cmath    │ cmath.isnan │ cmath.isinf    │ cmath.isfinite     │
├──────────┼─────────────┼────────────────┼────────────────────┤
│ numpy    │ numpy.isnan │ numpy.isinf    │ numpy.isfinite     │
╘══════════╧═════════════╧════════════════╧════════════════════╛

Again a couple of remarks:

  • The cmath and numpy functions also work for complex objects, they will check if either real or imaginary part is NaN or Infinity.
  • The numpy functions also work for numpy arrays and everything that can be converted to one [like lists, tuple, etc.]
  • There are also functions that explicitly check for positive and negative infinity in NumPy: numpy.isposinf and numpy.isneginf.
  • Pandas offers two additional functions to check for NaN: pandas.isna and pandas.isnull [but not only NaN, it matches also None and NaT]
  • Even though there are no built-in functions, it would be easy to create them yourself [I neglected type checking and documentation here]:

    def isnan[value]:
        return value != value  # NaN is not equal to anything, not even itself
    
    infinity = float["infinity"]
    
    def isinf[value]:
        return abs[value] == infinity 
    
    def isfinite[value]:
        return not [isnan[value] or isinf[value]]
    

To summarize the expected results for these functions [assuming the input is a float]:

╒════════════════╤═══════╤════════════╤═════════════╤══════════════════╕
│          input │ NaN   │ Infinity   │ -Infinity   │ something else   │
│ function       │       │            │             │                  │
╞════════════════╪═══════╪════════════╪═════════════╪══════════════════╡
│ isnan          │ True  │ False      │ False       │ False            │
├────────────────┼───────┼────────────┼─────────────┼──────────────────┤
│ isinf          │ False │ True       │ True        │ False            │
├────────────────┼───────┼────────────┼─────────────┼──────────────────┤
│ isfinite       │ False │ False      │ False       │ True             │
╘════════════════╧═══════╧════════════╧═════════════╧══════════════════╛

Is it possible to set an element of an array to NaN in Python?

In a list it's no problem, you can always include NaN [or Infinity] there:

>>> [math.nan, math.inf, -math.inf, 1]  # python list
[nan, inf, -inf, 1]

However if you want to include it in an array [for example array.array or numpy.array] then the type of the array must be float or complex because otherwise it will try to downcast it to the arrays type!

>>> import numpy as np
>>> float_numpy_array = np.array[[0., 0., 0.], dtype=float]
>>> float_numpy_array[0] = float["nan"]
>>> float_numpy_array
array[[nan,  0.,  0.]]

>>> import array
>>> float_array = array.array['d', [0, 0, 0]]
>>> float_array[0] = float["nan"]
>>> float_array
array['d', [nan, 0.0, 0.0]]

>>> integer_numpy_array = np.array[[0, 0, 0], dtype=int]
>>> integer_numpy_array[0] = float["nan"]
ValueError: cannot convert float NaN to integer

How do you check if a value is infinity in Python?

The math. isinf[] method checks whether a number is infinite or not. This method returns True if the specified number is a positive or negative infinity, otherwise it returns False.

What is NaN and INF in Python?

inf is infinity - a value that is greater than any other value. -inf is therefore smaller than any other value. nan stands for Not A Number, and this is not equal to 0 .

How do you know if a text is NaN in Python?

The math. isnan[] method checks whether a value is NaN [Not a Number], or not. This method returns True if the specified value is a NaN, otherwise it returns False.

How do you check if there are infinity values in Dataframe?

Method 1: Use DataFrame. isinf[] function to check whether the dataframe contains infinity or not. It returns boolean value. If it contains any infinity, it will return True.

Chủ Đề