-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
reindex fails on Dataset from MultiIndex DataFrame with RuntimeError #10347
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
Comments
Hmm I don't see any specific change on the Xarray side that would have caused this issue. However, the pandas behavior is not exactly the one that I would have expected: >>> import pandas as pd
>>> pd.__version__
'2.2.3'
>>> midx = pd.MultiIndex.from_product([[100, 200, 300], [1, 2, 3, 4]], names=["x", "y"])
>>> y_idx = midx.levels[1]
>>> y_idx
Index([1, 2, 3, 4], dtype='int64', name='y')
>>> y_idx.name = "y"
RuntimeError: Cannot set name on a level of a MultiIndex. Use 'MultiIndex.set_names' instead. It looks like Changing the line xarray/xarray/structure/alignment.py Line 123 in 07430e1
if pd_idx.name != k:
pd_idx = pd_idx.copy()
pd_idx.name = k should fix the issue. |
hi @benbovy , Thanks for the fix. I confirm that the fix works. Are you going to fix this in xarray (do you want the fix be merged)? Looking into pandas side, they also provide if pd_idx.name != k:
pd_idx = pd_idx.set_names(k) (it returns a copy, similar to your fix) Thanks again, |
Yes it would be nice to fix this in Xarray. Are you willing to submit a PR? I'm happy to review it. |
Yes, I'm willing to submit the PR soon. |
I've submitted a PR (#10381) for this. Thank you, |
What happened?
The reproducer code raises
RuntimeError: Cannot set name on a level of a MultiIndex. Use 'MultiIndex.set_names' instead.
from the last statement.
Traceback:
This happens with the latest xarray (
2025.4.0
). I could bisect the version for this issue, and the last version where it runs fine is2024.5.0
(and the next version, which is2024.6.0
breaks it).Thanks,
Zsolt
What did you expect to happen?
Code run without errors.
Minimal Complete Verifiable Example
MVCE confirmation
Relevant log output
Anything else we need to know?
No response
Environment
I'm using Linux on x86-64 with python 3.12.
The text was updated successfully, but these errors were encountered: