Something similar has been asked before (see Extract small data cubes around observation points in datacube, except that was slicing in label space instead of index space), but the answer is still the same: there’s no builtin way to do this, so one way or another you’ll have to loop manually. However, instead of indexing then concatenating xarray
objects, we can just as well slice a range object, construct an integer index and only then index the xarray
object:
def slice_indices(size, slices):
range_ = range(size)
return np.array([range_[slice_] for slice_ in slices])
dims = ["window_dim", "time"]
data.isel(time=xr.Variable(dims, slice_indices(data.sizes["time"], slices)))
For more advanced slicing, you might also be interested in xbatcher.