home / github / issue_comments

Menu
  • Search all tables
  • GraphQL API

issue_comments: 1239412326

This data as json

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-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

<xarray.DataArray (x: 4)>

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))

<xarray.DataArray (z: 2)>

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))

<xarray.DataArray (x: 1)>

array([0.08696024])

Coordinates:

* x (x) object MultiIndex

* y (x) float64 0.0

* z (x) int64 4

and

da.sel(y=0.0)

<xarray.DataArray (z: 1)>

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
Powered by Datasette · Queries took 0.875ms · About: xarray-datasette