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

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Ian. The Boolean or mask indexing is really powerful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.