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

Merged
merged 3 commits into from
Jun 20, 2025

Conversation

csernazs
Copy link
Contributor

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

@csernazs
Copy link
Contributor Author

Hi @benbovy ,

If you have time, could you please take another look at the fix? I changed the PR based on your suggestions.

Thanks for all the help,
Zsolt

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.

LGTM, thanks for the fix and the reminder @csernazs.

@benbovy
Copy link
Member

benbovy commented Jun 20, 2025

Merging... The failing test looks unrelated.

@benbovy benbovy merged commit 4d0300e into pydata:main Jun 20, 2025
31 of 33 checks passed
Copy link

welcome bot commented Jun 20, 2025

Congratulations on completing your first pull request! Welcome to Xarray! We are proud of you, and hope to see you again! celebration gif

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