潇潇雨雨
我会使用where, 以避免不得不下降到numpy:In [35]: dOut[35]: <xarray.DataArray (dim_0: 2, dim_1: 3)>array([[0, 1, 2], [3, 4, 5]])Dimensions without coordinates: dim_0, dim_1In [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如有必要,它将自动移动到浮动。