xarray icon indicating copy to clipboard operation
xarray copied to clipboard

resetting multiindex may be buggy

Open dcherian opened this issue 1 year ago • 1 comments

What happened?

Resetting a MultiIndex dim coordinate preserves the MultiIndex levels as IndexVariables. We should either reset the indexes for the multiindex level variables, or warn asking the users to do so

This seems to be the root cause exposed by https://github.com/pydata/xarray/pull/8809

cc @benbovy

What did you expect to happen?

No response

Minimal Complete Verifiable Example

import numpy as np
import xarray as xr

# ND DataArray that gets stacked along a multiindex
da = xr.DataArray(np.ones((3, 3)), coords={"dim1": [1, 2, 3], "dim2": [4, 5, 6]})
da = da.stack(feature=["dim1", "dim2"])

# Extract just the stacked coordinates for saving in a dataset
ds = xr.Dataset(data_vars={"feature": da.feature})
xr.testing.assertions._assert_internal_invariants(ds.reset_index(["feature", "dim1", "dim2"]), check_default_indexes=False) # succeeds
xr.testing.assertions._assert_internal_invariants(ds.reset_index(["feature"]), check_default_indexes=False) # fails, but no warning either

dcherian avatar Mar 28 '24 16:03 dcherian

Yes I think a proper fix would be to convert the variable data into a numpy array for each of the reset multi-index levels in .reset_index (https://github.com/pydata/xarray/pull/8672#issuecomment-1929837384).

benbovy avatar Mar 29 '24 07:03 benbovy