3

I know how to replace values based on a condition like here using .where but I would like to know how to replace all values in an xarray with a number.

Thanks in advance

Shruthi Patil
  • 77
  • 2
  • 8

2 Answers2

4

It might be more straight forward to use the following code:

a = xr.DataArray(np.arange(16).reshape(4, 4), dims=['x', 'y'])

c = a.where(a == 5, other=5)

r-beer
  • 121
  • 1
  • 1
  • 6
3

From the documentation here, all you'd have to do is:

a = xr.DataArray(np.arange(16).reshape(4, 4), dims=['x', 'y'])

b = a.where(a > 5, other=5)
b = b.where(b < 5, other=5)

As r-beer has suggested a direct approach would be to:

a = a.where(a == 5, other=5)
learner
  • 3,168
  • 3
  • 18
  • 35