Python what is nan

nan (not a number) in Python

In Python, the float type has nan . nan stands for «not a number» and is defined by the IEEE 754 floating-point standard.

In the sample code of this article, math, pandas, and NumPy are imported as follows.

import math import numpy as np import pandas as pd 

Note that None , which represents the absence of a value, is different from nan . For more information on None , see the following article.

See the following articles about how to remove and replace nan in NumPy and pandas.

nan is a float value in Python

In Python, the float type includes nan , which can be created using float(‘nan’) . Other creation methods will be described later.

print(float('nan')) # nan print(type(float('nan'))) # 

For example, if you read a CSV file in NumPy or pandas, the missing values are represented by nan ( NaN in pandas).

a = np.genfromtxt('data/src/sample_nan.csv', delimiter=',') print(a) # [[11. 12. nan 14.] # [21. nan nan 24.] # [31. 32. 33. 34.]] df = pd.read_csv('data/src/sample_pandas_normal_nan.csv')[:3] print(df) # name age state point other # 0 Alice 24.0 NY NaN NaN # 1 NaN NaN NaN NaN NaN # 2 Charlie NaN CA NaN NaN 

Create nan : float(‘nan’) , math.nan , numpy.nan

As described above, you can create nan with float(‘nan’) . It is case-insensitive, so you can use ‘NaN’ and ‘NAN’ .

print(float('nan')) # nan print(float('NaN')) # nan print(float('NAN')) # nan 

In addition, nan can be created by math (standard library) and NumPy; both NaN and NAN are defined as aliases in NumPy.

print(math.nan) # nan print(np.nan) # nan print(np.NaN) # nan print(np.NAN) # nan 

They are equivalent regardless of the method used for creation.

Check if a value is nan : math.isnan() , np.isnan()

You can check if a value is nan or not with math.isnan() .

print(math.isnan(float('nan'))) # True print(math.isnan(math.nan)) # True print(math.isnan(np.nan)) # True 

numpy.isnan() is also provided.

In addition to scalar values, array-like objects, such as lists and NumPy arrays ( ndarray ), can also be passed as arguments.

print(np.isnan(float('nan'))) # True print(np.isnan([float('nan'), math.nan, np.nan, 0])) # [ True True True False] 

pandas.DataFrame and Series have the method isna() and its alias isnull() , which return True for nan and None .

An error is raised if None is specified for math.isnan() or np.isnan() .

Behavior for comparison operators ( < , >, == , ! = ) with nan

When comparing with nan , < , >, == , = always return False , and != always returns True .

print(10  float('nan')) # False print(10 > float('nan')) # False print(10 == float('nan')) # False print(10 != float('nan')) # True 

The same is true for nan and nan comparisons. Note that == and != gives counter-intuitive results.

Any ordered comparison of a number to a not-a-number value is false. A counter-intuitive implication is that not-a-number values are not equal to themselves. For example, if x = float(‘NaN’), 3 < x, x < 3 and x == x are all false, while x != x is true. This behavior is compliant with IEEE 754. 6. Expressions - Value comparisons — Python 3.11.3 documentation

print(float('nan') == float('nan')) # False print(float('nan') != float('nan')) # True 

To check if a value is nan , use math.isnan() and numpy.isnan() instead of == .

Check nan in the if statement

In Python, objects other than True and False are also considered true or false in the conditional expression of the if statement. For example, the empty string » or the number 0 is considered false, and other strings or numbers are considered true.

As you can see with bool() , nan is evaluated as True .

Use math.isnan() or numpy.isnan() .

x = float('nan') if math.isnan(x): print('This is nan.') else: print('This is not nan.') # This is nan. 
x = 100 if math.isnan(x): print('This is nan.') else: print('This is not nan.') # This is not nan. 

Remove and replace nan in a list

If you want to remove or replace nan in a list, use list comprehensions, conditional expressions (ternary operators), and math.isnan() , numpy.isnan() .

l = [float('nan'), 0, 1, 2] print(l) # [nan, 0, 1, 2] print([x for x in l if not math.isnan(x)]) # [0, 1, 2] print([-100 if math.isnan(x) else x for x in l]) # [-100, 0, 1, 2] 

Just use math.isnan() and numpy.isnan() for check, and the concept is the same as other cases of removing and replacing values. See the following article for details.

See the following articles about how to remove and replace nan in NumPy and pandas.

Operations with nan

Operations such as + , — , * , / , and ** with nan result nan .

print(float('nan') + 100) # nan print(float('nan') - 100) # nan print(float('nan') - 100) # nan print(float('nan') / 100) # nan print(float('nan') ** 100) # nan 

Источник

How To Check NaN Value In Python

How To Check NaN Value In Python

in this post, We’ll learn how to check NAN value in python. The NaN stands for ‘Not A Number’ which is a floating-point value that represents missing data.

You can determine in Python whether a single value is NaN or NOT. There are methods that use libraries (such as pandas, math, and numpy) and custom methods that do not use libraries.

NaN stands for Not A Number, is one of the usual ways to show a value that is missing from a set of data. It is a unique floating-point value and can only be converted to the float type.

In this article, I will explain four methods to deal with NaN in python.

  • Check Variable Using Custom method
  • Using math.isnan() Method
  • Using numpy.nan() Method
  • Using pd.isna() Method

What is NAN in Python

None is a data type that can be used to represent a null value or no value at all. None isn’t the same as 0 or False, nor is it the same as an empty string. In numerical arrays, missing values are NaN; in object arrays, they are None.

Using Custom Method

We can check the value is NaN or not in python using our own method. We’ll create a method and compare the variable to itself.

def isNaN(num): return num!= num data = float("nan") print(isNaN(data))

Using math.isnan()

The math.isnan() is a Python function that determines whether a value is NaN (Not a Number). If the provided value is a NaN, the isnan() function returns True . Otherwise, False is returned.

Let’s check a variable is NaN using python script.

import math a = 2 b = -8 c = float("nan") print(math.isnan(a)) print(math.isnan(b)) print(math.isnan(c))

Using Numpy nan()

The numpy.nan() method checks each element for NaN and returns a boolean array as a result.

Let’s check a NaN variable using NumPy method:

import numpy as np a = 2 b = -8 c = float("nan") print(np.nan(a)) print(np.nan(b)) print(np.nan(c))

Using Pandas nan()

The pd.isna() method checks each element for NaN and returns a boolean array as a result.

The below code is used to check a variable NAN using the pandas method:

import pandas as pd a = 2 b = -8 c = float("nan") print(pd.isna(a)) print(pd.isna(b)) print(pd.isna(c))

Источник

Understanding NaN in Numpy and Pandas

Nans

NaN is short for Not a number. It is used to represent entries that are undefined. It is also used for representing missing values in a dataset.

The concept of NaN existed even before Python was created. IEEE Standard for Floating-Point Arithmetic (IEEE 754) introduced NaN in 1985.

NaN is a special floating-point value which cannot be converted to any other type than float.

In this tutorial we will look at how NaN works in Pandas and Numpy.

NaN in Numpy

Let’s see how NaN works under Numpy. To observe the properties of NaN let’s create a Numpy array with NaN values.

import numpy as np arr = np.array([1, np.nan, 3, 4, 5, 6, np.nan]) pritn(arr)

1. Mathematical operations on a Numpy array with NaN

Let’s try calling some basic functions on the Numpy array.

Let’ try finding the maximum from the array :

Thankfully Numpy offers methods that ignore the NaN values while performing Mathematical operations.

2. How to ignore NaN values while performing Mathematical operations on a Numpy array

Numpy offers you methods like np.nansum() and np.nanmax() to calculate sum and max after ignoring NaN values in the array.

If you have your autocompletion on in your IDE, you will see the following list of options while working with np.nan :

Np Nan

3. Checking for NaN values

To check for NaN values in a Numpy array you can use the np.isnan() method.

This outputs a boolean mask of the size that of the original array.

[False True False False False False True]

The output array has true for the indices which are NaNs in the original array and false for the rest.

4. Equating two nans

Are two NaNs equal to one another?

This can be a confusing question. Let’s try to answer it by running some python code.

These two statements initialize two variables, a and b with nan. Let’s try equating the two.

In Python we also have the is operator. Let’s try using that to compare the two variables.

The reason for this is that == operator compares the values of both the operands and checks for value equality. is operator , on the other hand, checks whether both the operands refer to the same object or not.

In fact, you can print out the IDs of both a and b and see that they refer to the same object.

NaN in Pandas Dataframe

Pandas DataFrames are a common way of importing data into python. Let’s see how can we deal with NaN values in a Pandas Dataframe.

Let’s start by creating a dataframe.

s = pd.DataFrame([(0.0, np.nan, -2.0, 2.0), . (np.nan, 2.0, np.nan, 1), . (2.0, 5.0, np.nan, 9.0), . (np.nan, 4.0, -3.0, 16.0)], . columns=list('abcd')) s

Dataframe

1. Checking for NaN values

You can check for NaN values by using the isnull() method. The output will be a boolean mask with dimensions that of the original dataframe.

Isnull

2. Replacing NaN values

There are multiple ways to replace NaN values in a Pandas Dataframe. The most common way to do so is by using the .fillna() method.

This method requires you to specify a value to replace the NaNs with.

Fillna0

Alternatively, you can also mention the values column-wise. That means all the NaNs under one column will be replaced with the same value.

values = s.fillna(value=values)

Fillna Column

You can also use interpolation to fill the missing values in a data frame. Interpolation is a slightly advanced method as compared to .fillna().

Interpolation is a technique with which you can estimate unknown data points between two known data points.

3. Drop rows containing NaN values

To drop the rows or columns with NaNs you can use the .dropna() method.

To drop rows with NaNs use:

To drop columns with NaNs use :

Conclusion

This tutorial was about NaNs in Python. We majorly focused on dealing with NaNs in Numpy and Pandas. Hope you had fun learning with us.

Источник

Читайте также:  Break питон как работает
Оцените статью