The xarray.Dataset.groupby
method prepares a dataset for iteration over subsets determined by the group
argument. If the group
argument is a constant value (i.e. one "subset" that's the whole dataset) broadcast to an xarray.DataArray
with matching coordinates, then I expect the first group returned to be identical to the original dataset. And that's what happens when the dataset coordinates are dimension coordinates. It doesn't happen for this dataset
ds_before = xr.Dataset(coords={'x': ('z', [0]), 'y': ('y', [1, 2])})
which prints as
Dimensions: (z: 1, y: 2)
Coordinates:
x (z) int64 0
* y (y) int64 1 2
Dimensions without coordinates: z
Data variables:
*empty*
Coordinate x
is a non-dimension coordinate. If you switched z
to x
above, then ds_after
produced by the following would be identical to ds_before
.
da = xr.DataArray(True, coords=ds_before.coords)
key, value = next(iter(ds.groupby(da)))
ds_after = value.unstack()
The printed representation of ds_after
is not identical to ds_before
.
Dimensions: (z: 1, y: 2)
Coordinates:
* z (z) int64 0
* y (y) int64 1 2
x (z, y) int64 0 0
Data variables:
*empty*
I can work around the new dimension coordinate z
, but I don't understand why x
has been broadcast along the y
dimension. Can you suggest a method for getting x
back to its original value?