0

When operating on different data-arrays, xarray is helpful enough to make sure the operation happens on values with the same coordinate. This is often desirable, but sometimes I want to explicitly operate on values with different coordinates. This will result in an empty DataArray:

In [275]: ds = xarray.DataArray(random.random(size=10), {"a": arange(10)}, dims=["a"])

In [274]: ds.sel(a=slice(5, None)) - ds.sel(a=slice(0, 4))
Out[274]: 
<xarray.DataArray (a: 0)>
array([], dtype=float64)
Coordinates:
  * a        (a) int64 

Of course, I could instead operate on the underlying .values, bypassing xarray, but is there a way to do this within the xarray API? I realise I would have to inform xarray what coordinates should be assigned to the result, but the dimensions, unaffected coordinates, name, encoding, and attributes could still be copied over.

My actual use case is to find instances where two A[i] & B[i+1] for boolean arrays A and B with identical coordinates, such as (A[:-1] & B[1:]), but xarrays helpful alignment makes this identical to (A & B)[1:-1], which has a different meaning.

But there are many possible applications:

  • Taking the difference between measurements taken at different times
  • Taking the difference between measurements with different lat/lons
5
  • What would you want the coordinate of the result to be? It seems a bit like you want to reject the coordinate labels and just join on location. Commented Feb 23, 2017 at 3:07
  • You could copy the DataArray, shift the coordinate, and then join? That way the old da and the new da would be 'off' by the shift Commented Feb 23, 2017 at 3:11
  • @Maximilian I realise I would need to inform the DataArray of the coordinate. In this case, I do want to reject the coordinate labels, I've so far settled for (x.variable & y.variable) but regrettably that loses all the other information as well. Commented Feb 23, 2017 at 11:52
  • Yes. What do you think of the 'separate / shift / join' option? Did that make sense? Commented Feb 23, 2017 at 22:49
  • @Maximilian Vaguely, but I'm not sure what it would look like in practice. I tried to play around with reindex and assign_coords but I'm not sure if those are the right methods. Commented Feb 24, 2017 at 0:53

1 Answer 1

1

One will have to do something like:

xarray.DataArray(a.variable - b.variable, coords=a.coords, name="Δ")

This still ensure that the dimensions are consistent (and will broadcast if not), but it does allow to calculate operations between values with different coordinates. Then the coords= keyword is essential to tell xarray what the coordinates of the result should be.

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

Comments

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.