0

With Python 3.6.5 and xarray 0.12.1, I have created a Dataset named ds_merge with two variables named v1 and v2 merged together.

I need to do computation on v1 based on values of v2.

print(ds_merge)

<xarray.Dataset>
Dimensions:  (x: 2, y: 3)
Coordinates:
  * x        (x) <U1 'a' 'b'
  * y        (y) <U1 'A' 'B' 'C'
Data variables:
    v1       (x, y) float64 0.8332 0.09855 1.477 0.6563 -0.1991 -0.9999
    v2       (x, y) bool True True True True False False

What I want to achieve is to multiply v1 by 10 only in the circumstance of v2 being True, such that ds_merge will be:

<xarray.Dataset>
Dimensions:  (x: 2, y: 3)
Coordinates:
  * x        (x) <U1 'a' 'b'
  * y        (y) <U1 'A' 'B' 'C'
Data variables:
    v1       (x, y) float64 8.332 0.9855 14.77 6.563 -0.1991 -0.9999
    v2       (x, y) bool True True True True False False
alextc
  • 3,206
  • 10
  • 63
  • 107

1 Answers1

1

Seems like xarray does not support 2-dim boolean indexing. But numpy does, so you can just do:


ds_merge['v1'].values[ds_merge['v2'].values] *= 10

Ian
  • 609
  • 3
  • 12