Skip to content

Fix reindex on Dataset from MultiIndex DataFrame with RuntimeError #10381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

csernazs
Copy link

Copy link

welcome bot commented May 30, 2025

Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient.
If you have questions, some answers may be found in our contributing guidelines.

Copy link
Member

@benbovy benbovy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @csernazs!

Just added a suggestion for the test, otherwise it looks good.

@@ -293,6 +293,35 @@ def test_reindex_like(self) -> None:
with pytest.raises(ValueError, match=r".*index has duplicate values"):
index3.reindex_like(index2)

def test_reindex_pandas_multiframe(self) -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A simpler test for the fixed issue would something like:

def test_reindex_with_multiindex_level(self) -> None:
    # test for https://github.com/pydata/xarray/issues/10347
    mindex = pd.MultiIndex.from_product([[100, 200, 300], [1, 2, 3, 4]], names=["x", "y"])
    y_idx = PandasIndex(mindex.levels[1], "y")

    ds1 = xr.Dataset(coords={"y": [1, 2, 3]})
    ds2 = xr.Dataset(coords=xr.Coordinates.from_xindex(y_idx))

    actual = ds1.reindex(y=ds2.y)
    assert_identical(actual, ds2)

I'd also move this test into test_dataset.py and leave test_indexes.py for testing the logic that is implemented in xarray.core.indexes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @benbovy for the review!

I have implemented the test as you suggested. I put the test to the end of the file, if that's not correct, let me know.

Zsolt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

reindex fails on Dataset from MultiIndex DataFrame with RuntimeError
2 participants