1

Please let me know if anyone knows of a better way to do about the following. I am trying to replace some values in numpy array. Replace condition is differ in each columns. Suppose I have a numpy array and list of nodata values like:

import numpy as np
array = np.array([[ 1, 2, 3],
                  [ 4, 5, 6],
                  [ 7, 8, 9],
                  [10,11,12]])
nodata_values = [4, 8, 3]

and what I want to have is array which values are replaced like

array([[ 1.,  2., nan],
       [nan,  5.,  6.],
       [ 7., nan,  9.],
       [10., 11., 12.]])

I know I can do this:

np.array([np.where(i == nodata_values[idx], np.nan, i) 
          for idx, i in enumerate(array.T)]).T

But this code using for loop inside therefore applying it to a table with tens of thousands of rows will takes time.

AMA
  • 174
  • 11
  • `nan` is a floating-point value, so your array can only contain it if the `dtype` is a floating-point type. – Karl Knechtel Aug 11 '20 at 14:38
  • Does this answer your question? [Replacing Numpy elements if condition is met](https://stackoverflow.com/questions/19766757/replacing-numpy-elements-if-condition-is-met) – jkr Aug 11 '20 at 14:39
  • @KarlKnechtel, thanks your comment, I know that and I don't mind float format. because I want to have np.nan value. – AMA Aug 11 '20 at 14:40
  • 1
    `mask = (array==4) || (array == 8) || (array == 3); array[mask] = np.nan` – jkr Aug 11 '20 at 14:40
  • @jakub, I want different values for each column for a replacement condition. – AMA Aug 11 '20 at 14:43

1 Answers1

1

Use np.isin to create boolean index & broadcast.

astype to avoid ValueError: cannot convert float NaN to integer

import numpy as np

array = array.astype(np.float)

array[np.isin(array , nodata_values)] = np.NaN

[[ 1.  2. nan]
 [nan  5.  6.]
 [ 7. nan  9.]
 [10. 11. 12.]]
sushanth
  • 8,275
  • 3
  • 17
  • 28