a = a[~np.isnan(a)]
Answer from mtrw on Stack OverflowNumPy
numpy.org › doc › stable › reference › generated › numpy.isnan.html
numpy.isnan — NumPy v2.5 Manual
NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic (IEEE 754). This means that Not a Number is not equivalent to infinity. ... Try it in your browser! >>> import numpy as np >>> np.isnan(np.nan) True >>> np.isnan(np.inf) False >>> np.isnan([np.log(-1.),1.,np.log(0)]) array([ ...
Top answer 1 of 4
205
a = a[~np.isnan(a)]
2 of 4
76
You are currently testing for anything that is not NaN and mtrw has the right way to do this. If you are interested in testing for finite numbers (is not NaN and is not INF) then you don't need an inversion and can use:
np.isfinite(a)
More pythonic and native, an easy read, and often when you want to avoid NaN you also want to avoid INF in my experience.
Just thought I'd toss that out there for folks.
Codecademy
codecademy.com › docs › python › math module › math.isnan()
Python | Math Module | math.isnan() | Codecademy
September 4, 2024 - In Python, the math.isnan() function returns True if the given value is NaN (not a number) and False if the value is a number.
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.isnan.html
numpy.isnan — NumPy v2.1 Manual
NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic (IEEE 754). This means that Not a Number is not equivalent to infinity. ... >>> import numpy as np >>> np.isnan(np.nan) True >>> np.isnan(np.inf) False >>> np.isnan([np.log(-1.),1.,np.log(0)]) array([ True, False, False])
W3Schools
w3schools.com › python › ref_math_isnan.asp
Python math.isnan() Method
The math.isnan() method checks whether a value is NaN (Not a Number), or not.
GeeksforGeeks
geeksforgeeks.org › python › check-for-nan-values-in-python
Check For NaN Values in Python - GeeksforGeeks
July 23, 2025 - NaN values are commonly encountered in data science and numerical computing when working with datasets that contain missing, undefined, or invalid values. The numpy.isnan() function is a simple way to check for NaN values in a NumPy array.
GeeksforGeeks
geeksforgeeks.org › python › numpy-isnan-python
numpy.isnan() in Python - GeeksforGeeks
March 8, 2024 - The numpy.isnan() function tests element-wise whether it is NaN or not and returns the result as a boolean array.
NumPy
numpy.org › devdocs › reference › generated › numpy.isnan.html
numpy.isnan — NumPy v2.6.dev0 Manual
NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic (IEEE 754). This means that Not a Number is not equivalent to infinity. ... Try it in your browser! >>> import numpy as np >>> np.isnan(np.nan) True >>> np.isnan(np.inf) False >>> np.isnan([np.log(-1.),1.,np.log(0)]) array([ ...
Omz Software
omz-software.com › pythonista › numpy › reference › generated › numpy.isnan.html
numpy.isnan — NumPy v1.8 Manual
Test element-wise for Not a Number (NaN), return result as a bool array.
Readthedocs
datatest.readthedocs.io › en › stable › how-to › nan-values.html
How to Deal With NaN Values — datatest 0.11.1 documentation
>>> import math >>> x = float('nan') >>> math.isnan(x) True
Reddit
reddit.com › r/learnpython › math.isnan()
r/learnpython on Reddit: math.isnan()
December 6, 2013 -
does this EVER return true? gives error on 1/0 and arcsin(2) and '123' and 'abc' and so forth. says it needs a float. but a float is a number, right?
Top answer 1 of 2
4
I think you're confused about what NaN represents. NaN is a special value of a IEEE-754 floating point number that signifies that the result of a floating point computation does not exist (the spec also defines representations for positive and negative infinity). math.isnan will check if a value is this special NaN value, not whether the provided value is of numeric type. In most cases where a NaN would be generated, Python chooses to raise an exception instead of returning NaN (you noticed this is the case for division by zero, but it also applies to taking the square root of a negative number or providing input outside of the domain of inverse trigonometric functions). This behavior differs from some other languages. See also: CPython implementation detail about NaN , Wikipedia article on NaN
2 of 2
3
A float can basically be either a number, positive or negative infinity, or NaN. You can think of NaN and the infinities as special values that a float can have. math.isnan() takes a float and tells you whether its value is NaN. The Python math library is a wrapper around a C library. The corresponding functions in the C library return NaN for many things, but the Python wrapper checks for most (maybe all) of these and raises an exception instead of returning NaN. This is done to make the math module fit in better with the rest of Python, which prefers to raise exceptions instead of returning special values. So, most of the time there's no use for math.isnan() in pure Python code (because Python raises exceptions instead of returning NaN). However, it may be needed in cases where Python code has to process data from external sources (which might produce NaN).
IncludeHelp
includehelp.com › python › isnotnan-functionality-in-numpy-can-this-be-more-pythonic.aspx
Python - 'isnotnan' functionality in numpy, can this be more pythonic?
October 9, 2023 - # Import numpy import numpy as np # Creating numpy array arr = np.array([np.nan, 1, 2]) # Display original array print("Orignal array:\n",arr,"\n") # Check for each value res = arr[~np.isnan(arr)] # Display result print("Result:\n",res,"\n")
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.isnan.html
numpy.isnan — NumPy v2.2 Manual
NumPy uses the IEEE Standard for Binary Floating-Point for Arithmetic (IEEE 754). This means that Not a Number is not equivalent to infinity. ... >>> import numpy as np >>> np.isnan(np.nan) True >>> np.isnan(np.inf) False >>> np.isnan([np.log(-1.),1.,np.log(0)]) array([ True, False, False])