html_url,issue_url,id,node_id,user,created_at,updated_at,author_association,body,reactions,performed_via_github_app,issue
https://github.com/pydata/xarray/issues/6838#issuecomment-1239509767,https://api.github.com/repos/pydata/xarray/issues/6838,1239509767,IC_kwDOAMm_X85J4WsH,4160723,2022-09-07T15:02:12Z,2022-09-07T15:02:12Z,MEMBER,"@nunupeke The `TypeError` in your example should be fixed in #7004, which also improves how slice objects are handled in general for a multi-index.","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,1319724885
https://github.com/pydata/xarray/issues/6838#issuecomment-1239412326,https://api.github.com/repos/pydata/xarray/issues/6838,1239412326,IC_kwDOAMm_X85J3-5m,4160723,2022-09-07T13:45:25Z,2022-09-07T13:45:25Z,MEMBER,"> only that the resulting coordinates look a bit weird, containing slices
I'm working on this issue right now and I see this too.
I don't think that providing slice objects to multi-index level coordinates in `.sel()` is something that so far we've really expected to work. We rather expect scalar values, and we explicitly raise a `ValueError` when trying to pass multiple values:
```python
da
#
# array([0.30120807, 0.43951659, 0.19163508, 0.57251755])
# Coordinates:
# * x (x) object MultiIndex
# * y (x) float64 0.0 0.3333 0.6667 1.0
# * z (x) int64 4 5 6 7
da.sel(z=[4, 5])
# ValueError: Vectorized selection is not available along coordinate 'z' (multi-index level)
```
The fact that it used to work with slices looks like a side effect. For example, providing a slice for only one of the level coordinates drops that coordinate in the resulting dataset (v2022.3.0):
```python
da.sel(y=slice(None, 0.5))
#
# array([0.4004091 , 0.11179854])
# Coordinates:
# * z (z) int64 4 5
#
# The 'y' coord is missing! It should be still there.
```
I think that we could support slices in a clean way (maybe any sequence of values too?) by reusing `pandas.MultiIndex.get_locs()` internally. However, in that case it would be hard to automatically collapse one or more multi-index levels, e.g.,
```python
# note the difference between
da.sel(y=0.0, z=slice(None))
#
# array([0.08696024])
# Coordinates:
# * x (x) object MultiIndex
# * y (x) float64 0.0
# * z (x) int64 4
# and
da.sel(y=0.0)
#
# array([0.08696024])
# Coordinates:
# * z (z) int64 4
# y float64 0.0
# the 1st one calls pandas.MultiIndex.get_locs, while
# the 2nd one calls pandas.MultiIndex.get_loc_level
```
","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,1319724885
https://github.com/pydata/xarray/issues/6838#issuecomment-1199204456,https://api.github.com/repos/pydata/xarray/issues/6838,1199204456,IC_kwDOAMm_X85Hemho,4160723,2022-07-29T12:11:23Z,2022-07-29T12:11:23Z,MEMBER,Thanks for the report @nunupeke. That's definitely a regression. I'm not that surprised actually as the logic behind `.sel()` is already quite convoluted for the case of (pandas) multi-indexes :-).,"{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,1319724885