Fix DataArray coordinate mismatch after shape-changing array wrap#11405
Open
C1-BA-B1-F3 wants to merge 1 commit into
Open
Fix DataArray coordinate mismatch after shape-changing array wrap#11405C1-BA-B1-F3 wants to merge 1 commit into
C1-BA-B1-F3 wants to merge 1 commit into
Conversation
…-changing ops like pinv - Route DataArray.__array_wrap__ through _replace_maybe_drop_dims to properly handle coordinates when shape changes - In Variable.__array_wrap__, detect when the last two dims are transposed (e.g. np.linalg.pinv) and swap dim names accordingly - Fall back to generic dim names for other shape changes - Add regression test for np.linalg.pinv on a rectangular DataArray Fixes pydata#11396 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
450e54d to
bfffe80
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
DataArray.__array_wrap__through_replace_maybe_drop_dimsto properly handle coordinates when shape changesVariable.__array_wrap__, detect when the last two dims are transposed (e.g.np.linalg.pinv) and swap dim names accordinglynp.linalg.pinvon a rectangular DataArrayFixes #11396
How it works
When
np.linalg.pinvis called on a DataArray of shape(3, 4)with dims(foo, bar):Variable.__array_wrap__detects the shape changed from(3, 4)to(4, 3)— the last two dims are transposed(bar, foo)DataArray.__array_wrap__calls_replace_maybe_drop_dims, which sees that sizes match after the dim swap and preserves all coordinatesThis approach correctly maps dimensions rather than just dropping mismatched coordinates, so coords like
foo: ['x', 'y', 'z']stay on thefoodimension (which is still size 3).Tests
🤖 Generated with Claude Code