Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NaN in python and validity checking [duplicate]

Tags:

python

nan

numpy

I'm doing a program where I optimize some values. Due to the equations, from time to time my values are NaN

My problem, some of the entries are NaN.

I would like to know if there is a test to check their logical validity so I can skip those values and retry.

So far I have tried checking for

a==np.nan, a==nan, b=a a==b

To no avail.

I hope you can help me

Thanks

like image 953
Leon palafox Avatar asked May 16 '11 11:05

Leon palafox


People also ask

How do you validate NaN in Python?

The math. isnan() method checks whether a value is NaN (Not a Number), or not. This method returns True if the specified value is a NaN, otherwise it returns False.

Can you compare to NaN in Python?

We cannot use the regular comparison operator, == , to check for NaN. NaN is not equal to anything (not even itself!).

Is NaN == NaN Python?

nan is NOT equal to nan nan == np. nan is False can trigger a reaction of confusion and frustration.

Is NaN true or false Python?

nan is always false in the output.


2 Answers

Using numpy,

import numpy as np
np.isnan(np.nan) # returns True
like image 88
riza Avatar answered Oct 08 '22 03:10

riza


Since Python 2.6, you want to import math and use math.isnan(a).

See http://docs.python.org/library/math.html#math.isnan

like image 20
n00dle Avatar answered Oct 08 '22 02:10

n00dle