Fix as_dataarray to apply coords parameter for DataArray input#551
Draft
FBumann wants to merge 2 commits intoPyPSA:masterfrom
Draft
Fix as_dataarray to apply coords parameter for DataArray input#551FBumann wants to merge 2 commits intoPyPSA:masterfrom
FBumann wants to merge 2 commits intoPyPSA:masterfrom
Conversation
Previously, when a DataArray was passed to as_dataarray(), the coords parameter was silently ignored. This was inconsistent with other input types (numpy, pandas) where coords are applied. Now, when coords is provided as a dict and the input is a DataArray, the function will reindex the array to match the provided coordinates. This ensures consistent behavior across all input types. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2. Expands to new dims from coords (broadcast) Summary: - as_dataarray now consistently applies coords for all input types - DataArrays with fewer dims are expanded to match the full coords specification - This fixes the inconsistency when creating variables with DataArray bounds
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.
Fix as_dataarray to apply coords parameter for DataArray input
Summary
Fixes an API inconsistency in
as_dataarray()where thecoordsparameter was silently ignored when the input was already a DataArray.API Change
Before (broken)
After (fixed)
Behavior by input type
coordsparam (before)coordsparam (after)np.ndarraypd.SeriesDataArrayWhat the fix does for DataArray input
When
coordsis a dict:expand_dims)Extra dimensions in the DataArray (not in
coords) are preserved.Breaking Change
This is technically a breaking change for code that:
as_dataarray()with acoordsparametercoordsparameter being silently ignoredMigration: If you need the old behavior (pass-through), don't pass the
coordsparameter.In practice, this is a bug fix - if you passed
coords, you likely expected them to be applied.Edge Cases
coordscoordscoordscoordsis a list (not dict)Checklist