-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
Code Sample
import xarray as xr
d = xr.DataArray([0], coords={'coord': ('dim', [0])}, dims=['dim'])
d.set_index(append=True, inplace=True, dim=['coord'])
d.sel(dim=0) # works
d.sel(coord=0) # doesn't work, coord does not exist anymore
print(d)
<xarray.DataArray (dim: 1)>
array([0])
Coordinates:
* dim (dim) int64 0
Problem description
when a DataArray is initialized with a dimension containing only one coordinate, selection on the coordinate is not directly possible. As a workaround, we can set_index
but if there is only one coordinate on a dimension, the coordinate vanishes and its values are attached to the dimension directly.
The DataArrays in my use case are generic, in some cases there are multiple coordinates and sometimes there is only one. If the one consistent coordinate is discarded for some cases, follow-up code becomes tedious.
Having a single-coordinate MultiIndex would be much more intuitive so that one can still .sel
over the coordinate.
Expected Output
<xarray.DataArray (dim: 1)>
array([0])
Coordinates:
* dim MultiIndex
coord (dim) int64 0
For more than one coordinate on the dimension, the dimension becomes a MultiIndex with all the coordinates. With only a single coordinate however, this does not happen.