-
I often find myself with a DataArray with a mutlindexed dimension that I want to add and/or remove levels from before unstacking. I have developed some ways to do this, but they are somewhat complicated. I wanted to make sure there wasn't a better way to do this.
Here is the method I use to add a level to this multindex.
I can also remove a level, but It's the same idea.
I'm using It feels this should be all just handled by the 'assign_coords' function, but it appears I have to set and reset the index. This is OK but somewhat cumbersome because I have to explicitly write out the level names. perhaps there could be |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You actually don't need to first reset the index. You can add a level with
Or remove a level with However, I wouldn't rely too much on that. Xarray indexes are currently being refactored and the API above might be depreciated eventually. |
Beta Was this translation helpful? Give feedback.
You actually don't need to first reset the index.
You can add a level with
set_index(..., append=True)
:Or remove a level with
mda_new.reset_index(x="one", drop=True)
However, I wouldn't rely too much on that. Xarray indexes are currently being refactored and the API above might be depreciated eventually.