Not a Number

Not a Number (NaN) are a special state that indicates an undefined value. NaNs are only defined for floating point variables.

The following python functions exists and can be used to test if a value is NaN:

The following mathematical functions exists that can be used with arrays that contain NaNs:

Example

In [23]: a=arange(4,dtype=float)

In [24]: a[1]=nan

In [25]: i=isfinite(a)

In [26]: a
Out[26]: array([  0.,  NaN,   2.,   3.])

In [27]: i
Out[27]: array([ True, False,  True,  True], dtype=bool)

In [28]: a[i]
Out[28]: array([ 0.,  2.,  3.])

In [29]: mean(a[i])
Out[29]: 1.6666666666666667

In [30]: mean(a)
Out[30]: nan

In [31]: import scipy.stats

In [32]: scipy.stats.nanmean(a)
Out[32]: 1.6666666666666667

LehreWiki: OpenSource2010/Lesson11 (last edited 2011-01-17 10:56:34 by anonymous)