xarray:仅用 1 替换正值

问题描述

我有一个 xarray,它看起来像:

component_list = ['c1','c2']  
space_list = ['01_reg','02_reg','03_reg','04_reg','05_reg']

data = np.array([ [np.nan for i in range(5)],[3.0,3.0,0.0,0.0] 
                   ])

xr = xr.DataArray(data,coords=[component_list,space_list],dims=['component','space'])  

我想将所有正值替换为 1,将 0s 保留为 0s,将 nas 保留为 nas。我尝试了以下但 nas 也变成了 1s:

xr.where(xr>0,1)

here 之前已经问过这个问题,但建议的解决方案不起作用。我怎样才能做到这一点?

解决方法

一种解决方案是查找所有空值和 0 值并执行逻辑或。这将是一个条件。如果此条件不成立,则将 value 替换为 1。

xr.where(np.logical_or(xr.isnull(),xr==0),1)