pydata / pydata/xarray

`isel(multi_index_level_name = MultiIndex.level)` corrupts the MultiIndex

Open
#8,952 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-indexing
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

What happened?

From https://github.com/pydata/xarray/discussions/8951

if d is a MultiIndex-ed dataset with levels (x, y, z), and m is a dataset with a single coord x
m.isel(x=d.x) builds a dataset with a MultiIndex with levels (y, z). This seems like it should work.

cc @benbovy

What did you expect to happen?

No response

Minimal Complete Verifiable Example
import pandas as pd, xarray as xr, numpy as np

xr.set_options(use_flox=True)

test = pd.DataFrame()
test["x"] = np.arange(100) % 10
test["y"] = np.arange(100)
test["z"] = np.arange(100)
test["v"] = np.arange(100)

d = xr.Dataset.from_dataframe(test)
d = d.set_index(index = ["x", "y", "z"])
print(d)

m = d.groupby("x").mean()
print(m)

print(d.xindexes)
print(m.isel(x=d.x).xindexes)

xr.align(d, m.isel(x=d.x))
#res = d.groupby("x") - m
#print(res)
<xarray.Dataset>
Dimensions:  (index: 100)
Coordinates:
  * index    (index) object MultiIndex
  * x        (index) int64 0 1 2 3 4 5 6 7 8 9 0 1 2 ... 8 9 0 1 2 3 4 5 6 7 8 9
  * y        (index) int64 0 1 2 3 4 5 6 7 8 9 ... 90 91 92 93 94 95 96 97 98 99
  * z        (index) int64 0 1 2 3 4 5 6 7 8 9 ... 90 91 92 93 94 95 96 97 98 99
Data variables:
    v        (index) int64 0 1 2 3 4 5 6 7 8 9 ... 90 91 92 93 94 95 96 97 98 99
<xarray.Dataset>
Dimensions:  (x: 10)
Coordinates:
  * x        (x) int64 0 1 2 3 4 5 6 7 8 9
Data variables:
    v        (x) float64 45.0 46.0 47.0 48.0 49.0 50.0 51.0 52.0 53.0 54.0
Indexes:
  ┌ index    PandasMultiIndex
  │ x
  │ y
  └ z
Indexes:
  ┌ index    PandasMultiIndex
  │ y
  └ z
ValueError...
MVCE confirmation
  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • New issue — a search of GitHub Issues suggests this is not a duplicate.
  • Recent environment — the issue occurs with the latest version of xarray and its dependencies.
Relevant log output

No response

Anything else we need to know?

No response

Environment

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided xarray MVCE and inspect m.isel(x=d.x), the resulting xindexes, and xr.align. Trace the isel path handling the MultiIndex level x. Done means selecting with d.x preserves a valid MultiIndex with levels y and z and xr.align no longer raises ValueError.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, pandas, python
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.