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
DataArrayof 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.reindexandassign_coordsbut I'm not sure if those are the right methods.