Skip to content

Fix unstacking after selecting stacked features - #11808

Merged
dcherian merged 2 commits into
pydata:mainfrom
AryanRG:fix-unstack-selected-features
Oct 27, 2026
Merged

dcherian merged 2 commits into
pydata:mainfrom
AryanRG:fix-unstack-selected-features

Conversation

@AryanRG

@AryanRG AryanRG commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Description

Selecting features from a stacked array can leave unused variable labels in its index. to_unstacked_dataset() then tries to select those missing variables and raises KeyError.

This removes unused labels when finding the variables to unstack. Tests cover NumPy and Dask arrays, different level selections, and preservation of the input and coordinate metadata.

Regression and nearby stacking tests pass, as do the configured pre-commit hooks. The local type check reports the same errors on the unchanged base.

Checklist

  • Tests added
  • User visible changes (including notable bug fixes) are documented in whats-new.rst

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR.
    • I take responsibility for any AI-generated content in my PR.
      Tools: Codex, used for implementation, tests, review and the description. Task: fix unstacking after feature selection while preserving the selected data and metadata.

Co-authored-by: Codex <noreply@openai.com>

@headtr1ck headtr1ck left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, this looks good! PandasMultiIndex.isel keeps pandas' unused levels, and unstack / from_dataframe already drop them with remove_unused_levels_categories, so doing the same here is consistent.

Could you add the PR number and your name to the whats-new entry, like the other entries?

- :py:meth:`DataArray.to_unstacked_dataset` no longer raises ``KeyError`` for
  unused variable labels left in the stacked index after selecting features
  (:pull:`11808`).
  By `Aryan R. Ganjoo <https://github.com/AryanRG>`_.

Co-authored-by: Codex <noreply@openai.com>
@AryanRG

AryanRG commented Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

Added the PR number and author credit, thanks!

[Codex on behalf of AryanRG.]

@headtr1ck headtr1ck left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks!

@headtr1ck headtr1ck added the plan to merge Final call for comments label Oct 26, 2026
@dcherian
dcherian merged commit 423db79 into pydata:main Oct 27, 2026
62 checks passed
@welcome

welcome Bot commented Oct 27, 2026

Copy link
Copy Markdown

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

Labels

plan to merge Final call for comments

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants