4

enter image description here

I used the temp[temp==0] = np.nan, but I got this Error:

IndexError: 2-dimensional boolean indexing is not supported.

2
  • could you please let us know what you tried and what you want. Question must be more precise fro us to help
    – Myjab
    Oct 3, 2018 at 15:39
  • I want to convert zero value to Nan in the array.
    – LEO
    Oct 3, 2018 at 15:41

1 Answer 1

7

I'd use where, to avoid having to drop down to numpy:

In [35]: d
Out[35]: 
<xarray.DataArray (dim_0: 2, dim_1: 3)>
array([[0, 1, 2],
       [3, 4, 5]])
Dimensions without coordinates: dim_0, dim_1

In [36]: d.where(d != 0)
Out[36]: 
<xarray.DataArray (dim_0: 2, dim_1: 3)>
array([[nan,  1.,  2.],
       [ 3.,  4.,  5.]])
Dimensions without coordinates: dim_0, dim_1

and which will automatically move to floats if necessary.

2
  • Great! Thanks for your reply.
    – LEO
    Oct 4, 2018 at 3:38
  • @DSM Is there a way to perform this conversion by preserving the values as integers? I don't want them to be moved to float. Jul 31, 2022 at 21:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.