13

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

riza
  • 16,274
  • 7
  • 29
  • 29
Leon palafox
  • 2,675
  • 6
  • 27
  • 35

2 Answers2

30

Using numpy,

import numpy as np
np.isnan(np.nan) # returns True
eat
  • 7,440
  • 1
  • 19
  • 27
riza
  • 16,274
  • 7
  • 29
  • 29
7

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

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

Asclepius
  • 57,944
  • 17
  • 167
  • 143
n00dle
  • 5,949
  • 2
  • 35
  • 48
  • 1
    Any reason why one would want to use `math` instead of `numpy`? – dmn Oct 11 '11 at 15:20
  • Actually, I may have found [the answer](http://stackoverflow.com/questions/1322380/gotchas-where-numpy-differs-from-straight-python) to my own question :) – dmn Oct 11 '11 at 15:29