Updated April 19, 2023
Introduction to NumPy NaN
In Python, NumPy NAN stands for not a number and is defined as a substitute for declaring value which are numerical values that are missing values in an array as NumPy is used to deal with arrays in Python and this can be initialized using numpy.nan and in NumPy NaN is defined automatically to replace the value in a data frame in which the values are missing or not mentioned in such cases in the data frame we can write as NaN or nan as a placeholder to represent the missing data in a data frame which is a floating-point number.
Working of NumPy NaN in Python
In Python, NumPy with the latest version where nan is a value only for floating arrays only which stands for not a number and is a numeric data type which is used to represent an undefined value. In Python, NumPy defines NaN as a constant value. As we know in numeric data type we can use to represent only real numbers therefore whenever we want to compute any value such as the square root of negative numbers which would result in imaginary values and therefore we can use nan to represent such values in data frames.
In NumPy uses nan to assign variables which do not have any values and are needed to be computed. But we should note that in Python NaN is not similar to infinity and we can create NaN values also using float and numpy.nan.
Examples
Below are the examples of how to create or initialize the array with nan values in Python programs.
Example #1
We can create nan using float data type and can found in the math module also but only in the Python 3.5 plus version.
Code:
print("The nan can be created using float data type as follows")
ex_a1 = float("NaN")
print("\n")
print("The nan value will be printed as")
print(ex_a1)
print("\n")
print(type(ex_a1))
Output:
In the above program, we can see we have created a variable with float type and specified it as “nan”. We can see in the above screenshot the value of the variable is shown as “nan”. We should note that when we are specifying nan value in this float type its value is not case sensitive which means when we are specifying the nan value it can be written as “NaN” or “Nan” or “nan” or “NAN” will result in only “nan” value.
As we can also assign or create nan using math module in Python 3.5 and above version we can write the code as below:
Code:
import math
print("program to demonstrate the assigning nan values using math module")
print("\n")
ex_n = math.nan
print(ex_n)
print("\n")
if(math.isnan(ex_n)):
print("Yes it is nan")
Output:
In the above program, we can see we are first importing the math module, then we have created a variable and assigning nan value using the math module and printing the value of that variable. Then we are trying to cross-check if the variable contains nan value and that can be done using math module isnan() which returns true if the value is nan, else it returns false. We should remember these functions of math is only available in Python 3.5 and above versions.
Example #2
Now let us see how we can use nan or assign nan in an array using the NumPy module in Python with the example below.
Code:
import numpy as nn
print("program to demonstrate numpy array to assign the item values as nan")
print("\n")
nn_arr = nn.array(["educba", "Python", nn.nan])
print(nn_arr)
print(type(nn_arr))
print("\n")
nn_arr1 = nn.array([45, 7, 67, nn.nan])
print(nn_arr1)
print(type(nn.nan))
Output:
In the above program, we can see we have firstly imported the NumPy module, then we are using the alias name of NumPy to initialize nan value in the array items when creating an array using NumPy.array() function. We can observe in the above screenshot when we have created an array with integer item values are converted and we get the array in the output as float item values and hence we can say nan is a floating data type when we create these nan values in an array. Therefore we can see when we are printing the type of nn.nan we can in the above screenshot results in float data type.
Example #3
Now let us see what will be the impact of nan on the mathematical operations when we are trying to get the sum of the elements of the array containing a nan value and also the product of the elements having nan value. Let us demonstrate this in the below program using the NumPy module.
Code:
import numpy as nn
a_res = nn.sum(nn.array([10, 20, nn.nan]))
print("The sum of the elements in the given array is as follows:")
print(a_res)
print("\n")
nn_arr = nn.array([2, 3, nn.nan])
p_res = nn.multiply(nn_arr[0], nn_arr[2])
print("The product of the elements in the given array is as follows:")
print(p_res)
print("\n")
Output:
In the above program, we can see when we are having nan values in any data frames or arrays in Python using NumPy we can assign nan values and now when we are calculating the sum of the elements in an array having a nan value as one of the elements in the array then we get the sum as “nan” and even when we are trying to find the product of any elements with nan valued element in an array then it also results in “nan” as product value as seen in the above screenshot.
Example #4
As we can use isnan() function in math module to check for nan values of the created variable in a similar way we can also check for the array elements if it nan value or not using the same isnan() function in NumPy module.
Code:
import numpy as nn
nn_arr = nn.array([2, 3, nn.nan])
print("The array is as follows:")
print(nn_arr)
print("\n")
print("hecking for the values of the elements in an array if it is nan ")
print(nn.isnan(nn_arr))
Output:
In the above program, we can see that we have created an array with the third element as a nan value in it. Therefore when we are checking for the item values in an array for nan value we get true at the third index and the other indexes we get false as seen in the above screenshot.
Conclusion
In this article, we conclude that the nan which is “not a number” value assigned when we are not knowing the value of the items or variables declared. This nan value is used when there are missing values in any data frames or arrays. In this article, we saw how we can assign nan in NumPy and we also saw in the NumPy module it is a floating data type in Python. In this article, we also saw other than NumPy we can also use the math module but only in Python 3.5 and above version and hence we use the NumPy module in python for arrays and we also saw how the nan value affect in the mathematical operation on the array using NumPy in Python.
Recommended Articles
This is a guide to NumPy NaN. Here we discuss the introduction and working of NumPy NaN in python with examples respectively. You may also have a look at the following articles to learn more –