0

I've got a numpy array that contains data, but I want to replace some certain values with NA. I don't want to deal with masked data so I don't mask it. Is this possible?

My data is a numpy array with data type uint16:

array([[[1, 1, 1, ..., 1, 1, 1],
        [1, 1, 1, ..., 1, 1, 1],
        [1, 1, 1, ..., 1, 1, 1],
        ...,
        [1, 1, 1, ..., 1, 1, 1],
        [1, 1, 1, ..., 1, 1, 1],
        [1, 1, 1, ..., 1, 1, 1]]], dtype=uint16)

I want to replace those "1"s with NA values, I have used the code below, but I'm getting this error:

aa [aa == 1] = np.nan
ValueError: cannot convert float NaN to integer
Kaveh P. Yousefi
  • 165
  • 1
  • 2
  • 13

2 Answers2

2

type(np.nan) returns <class 'float'>.

You need your array to be of float type.

See: Numpy integer nan

Spoutnovitch
  • 119
  • 10
1

You try to put NaN values into an array of type uint16 which is a type that doesn't know a NaN value. Try the same with float32 instead and it will work.

Alfe
  • 56,346
  • 20
  • 107
  • 159