numpy.nan_to_num works fine on scalars.
>>> import numpy as np
>>> np.nan_to_num(float('inf'))
1.7976931348623157e+308
>>> np.nan_to_num(float('nan'))
0.0
>>> np.nan_to_num(float('-inf'))
-1.7976931348623157e+308
Answer from senshin on Stack Overflownumpy.nan_to_num works fine on scalars.
>>> import numpy as np
>>> np.nan_to_num(float('inf'))
1.7976931348623157e+308
>>> np.nan_to_num(float('nan'))
0.0
>>> np.nan_to_num(float('-inf'))
-1.7976931348623157e+308
You can just check if your variable of choice is NaN with math.isnan. If it is, change it to the number of your choice. Like this:
>>> import math
>>> x = float("nan")
>>> x
nan
>>> if math.isnan(x):
... x = 123 # just an example
...
>>> x
123
No, you can't, at least with current version of NumPy. A nan is a special value for float arrays only.
There are talks about introducing a special bit that would allow non-float arrays to store what in practice would correspond to a nan, but so far (2012/10), it's only talks.
In the meantime, you may want to consider the numpy.ma package: instead of picking an invalid integer like -99999, you could use the special numpy.ma.masked value to represent an invalid value.
a = np.ma.array([1,2,3,4,5], dtype=int)
a[1] = np.ma.masked
masked_array(data = [1 -- 3 4 5],
mask = [False True False False False],
fill_value = 999999)
A nan is a floating point only thing, there is no representation of it in the integers, so no :)
Pick an invalid value, like -99999
A[A==NDV]=numpy.nan
A==NDV will produce a boolean array that can be used as an index for A
You can also use np.where to replace a number with NaN.
arr = np.where(arr==NDV, np.nan, arr)
For example, the following result can be obtained via
arr = np.array([[1, 1, 2], [2, 0, 1]])
arr = np.where(arr==1, np.nan, arr)

This creates a new copy (unlike A[A==NDV]=np.nan) but in some cases that could be useful. For example, if the array was initially an int dtype, it will have to converted into a float array anyway (because replacing values with NaN won't work otherwise) and np.where can handle that.