Given the xarray below, I would like to add 10 to all negative values (i.e, -5 becomes 5, -4 becomes 6 ... -1 becomes 9, all values remain unchanged).
a = xr.DataArray(np.arange(25).reshape(5, 5)-5, dims=("x", "y"))
I tried:
a[a<0]=10+a[a<0]
, but it returns2-dimensional boolean indexing is not supported
.- Several attempts with
a.where
, but it seems that theother
argument can only replace the mapped values with a constant rather than with indexed values.
I also considered using numpy as suggested here, but my actual dataset is ~ 80 Gb and loaded with dask and using numpy crashes my Jupyter console.
Is there any way to achieve this using only xarray
?
Update
I updated the code using @SpaceBurger and this. However my initial example was using a DataArray whereas my true problem is using a Dataset:
a = xr.DataArray(np.arange(25).reshape(5, 5)-5, dims=("x", "y"))
a = a.to_dataset(name='variable')
Now, if I do this:
a1 = a['variable']
a2 = 10+a1.copy()
a['variable'] = dask.array.where(a['variable'] < 0, a2, a1)
I get this error:
MissingDimensionsError: cannot set variable 'variable' with 2-dimensional data without explicit dimension names. Pass a tuple of (dims, data) instead.
Can anyone suggest a proper syntax?