EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

NumPy NaN

Home » Software Development » Software Development Tutorials » NumPy Tutorial » NumPy NaN

NumPy NaN

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 with Examples

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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 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))

Popular Course in this category
Pandas and NumPy Tutorial (4 Courses, 5 Projects)4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (4,870 ratings)
Course Price

View Course

Related Courses
Python Training Program (36 Courses, 13+ Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

Output:

NumPy NaN 1

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:

using math module in Python

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:

NumPy NaN 3

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:

NumPy NaN 4

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:

we can also check for the array elements

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 –

  1. numpy.linspace()
  2. NumPy.argmax()
  3. NumPy Linear Algebra
  4. Matrix in NumPy

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
NumPy Tutorial
  • Basics
    • Introduction to NumPy
    • Install NumPy
    • NumPy Data Types
    • NumPy Functions
    • NumPy Histogram
    • numPy.where()
    • numpy.sort
    • NumPy floor()
    • Matrix in NumPy
    • Matrix Multiplication in NumPy
    • NumPy Arrays
    • NumPy Array Functions
    • NumPy Ndarray
    • NumPy Array Append
    • NumPy empty array
    • NumPy ndarray tolist
    • numpy.linspace()
    • NumPy.argmax()
    • NumPy Linear Algebra
    • numpy.diff()
    • numpy.unique( )
    • NumPy zeros
    • numpy.mean()
    • numpy.dot()
    • Numpy.argsort()
    • numpy.pad()
    • numpy.ravel()
    • NumPy stack
    • NumPy vstack
    • NumPy hstack
    • NumPy sum
    • NumPy cumsum
    • NumPy max
    • NumPy squeeze
    • NumPy median
    • NumPy Round
    • NumPy NaN
    • NumPy divide
    • NumPy square
    • NumPy ones
    • NumPy Meshgrid
    • NumPy exponential
    • NumPy percentile
    • NumPy fft
    • NumPy flatten
    • NumPy Concatenate
    • NumPy Outer
    • NumPy zip
    • NumPy Newaxis
    • NumPy Format
    • Numpy Random Seed ()
    • NumPy random choice
    • NumPy random normal
    • NumPy unravel_index
    • Numpy.eye()
    • NumPy append
    • NumPy searchsorted
    • NumPy shape
    • NumPy reshape
    • NumPy savetxt
    • NumPy genfromtxt
    • NumPy standard deviation
    • NumPy covariance
    • NumPy repeat
    • NumPy?Tile
    • NumPy Broadcasting

Related Courses

Pandas And NumPy Training Course

Python Training Course

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Pandas And NumPy Training Course Learn More