[Python] Numpy - Replace a number with NaN

Discussão em 'Python' iniciado por Stack, Abril 12, 2025.

  1. Stack

    Stack Membro Participativo

    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 seen 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 but numpy requires 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?

    Continue reading...

Compartilhe esta Página