如何将数组中的零转换为南?

How to convert Zero to Nan in the array?

我使用了 temp[temp==0] = np.nan,但我得到了这个错误:

IndexError: 2-dimensional boolean indexing is not supported.

我会使用 where,以避免不得不下降到 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

必要时会自动移动到浮动。