- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Test element-wise for NaN in Numpy
To test element-wise for NaN, use the numpy.isnan() method in Python Numpy. Returns True where x is NaN, false otherwise. This is a scalar if x is a scalar. The condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default out=None, locations within it where the condition is False will remain uninitialized.
NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic (IEEE 754). This means that Not a Number is not equivalent to infinity.
Steps
At first, import the required library −
import numpy as np
To test element-wise for NaN, use the numpy.isnan() method in Python Numpy.
Checking for numbers −
print("Check for NaN? ", np.isnan(1)) print("
Check for NaN? ", np.isnan(0))
Checking for float −
print("
Check for NaN? ", np.isnan(14.)) print("
Check for NaN? ", np.isnan(3.6))
Checking for NaN −
print("
Check for NaN? ", np.isnan(np.nan)) print("
Check for NaN? ", np.isnan(np.NAN))
Checking for infinity −
print("
Check for NaN? ", np.isnan(np.inf)) print("
Check for NaN? ", np.isnan(np.NINF))
Checking for log −
print("
Check for NaN? ", np.isnan(np.log(1))) print("
Check for NaN? ", np.isnan(np.log(2)))
Example
import numpy as np # To test element-wise for NaN, use the numpy.isnan() method in Python Numpy print("Check for NaN? ", np.isnan(1)) print("
Check for NaN? ", np.isnan(0)) # Checking for float print("
Check for NaN? ", np.isnan(14.)) print("
Check for NaN? ", np.isnan(3.6)) # Checking for NaN print("
Check for NaN? ", np.isnan(np.nan)) print("
Check for NaN? ", np.isnan(np.NAN)) # Checking for infinity print("
Check for NaN? ", np.isnan(np.inf)) print("
Check for NaN? ", np.isnan(np.NINF)) # Checking for log print("
Check for NaN? ", np.isnan(np.log(1))) print("
Check for NaN? ", np.isnan(np.log(2)))
Output
Check for NaN? False Check for NaN? False Check for NaN? False Check for NaN? False Check for NaN? True Check for NaN? True Check for NaN? False Check for NaN? False Check for NaN? False Check for NaN? False
- Related Articles
- Test element-wise for positive or negative infinity in Numpy
- Test element-wise for NaT (not a time) in Numpy
- Test array values for NaN in Numpy
- Compare two arrays with some NaN values and return the element-wise minimum in Numpy
- Subtract arguments element-wise in Numpy
- True Divide arguments element-wise in Numpy
- Test array values for NaN and store the result in a new location in Numpy
- Return element-wise string concatenation for two arrays of string in Numpy
- Calculate the absolute value element-wise in Numpy
- Return element-wise string multiple concatenation in Numpy
- Compute the bit-wise AND of two arrays element-wise in Numpy
- Compute the bit-wise NOT of an array element-wise in Numpy
- Compute the bit-wise OR of two Numpy arrays element-wise
- Compute the bit-wise XOR of two Numpy arrays element-wise
- Compute the bit-wise OR of two boolean arrays element-wise in Numpy
