Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy - Replace a number with NaN

I am looking to replace a number with NaN in numpy and am looking for a function like numpy.nan_to_num, except in reverse.

The number is likely to change as different arrays are processed because each can have a uniquely define NoDataValue. I have see people using dictionaries, but the arrays are large and filled with both positive and negative floats. I suspect that it is not efficient to try to load all of these into anything to create keys.

I tried using the following and numpy requiring that I use any() or all(). I realize that I need to iterate element wise, but hope that a built-in function can achieve this.

def replaceNoData(scanBlock, NDV):     for n, i in enumerate(array):         if i == NDV:             scanBlock[n] = numpy.nan 

NDV is GDAL's no data value and array is a numpy array.

Is a masked array the way to go perhaps?

like image 716
Jzl5325 Avatar asked Jul 15 '11 01:07

Jzl5325


People also ask

How do I replace 0 with NaN Numpy?

nan_to_num() in Python. numpy. nan_to_num() function is used when we want to replace nan(Not A Number) with zero and inf with finite numbers in an array. It returns (positive) infinity with a very large number and negative infinity with a very small (or negative) number.

Is NP NaN == NP NaN?

nan is NOT equal to nan At first, reading that np. nan == np. nan is False can trigger a reaction of confusion and frustration.

Does Numpy support NaN?

In Python, NumPy with the latest version where nan is a value only for floating arrays only which stands for not a number and is a numeric data type which is used to represent an undefined value. In Python, NumPy defines NaN as a constant value.


1 Answers

A[A==NDV]=numpy.nan 

A==NDV will produce a boolean array that can be used as an index for A

like image 109
Paul Avatar answered Oct 08 '22 20:10

Paul