Fix Dataset.map to preserve attrs set by the applied function#11406
Open
C1-BA-B1-F3 wants to merge 1 commit into
Open
Fix Dataset.map to preserve attrs set by the applied function#11406C1-BA-B1-F3 wants to merge 1 commit into
C1-BA-B1-F3 wants to merge 1 commit into
Conversation
|
Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient. |
Previously, when a function passed to Dataset.map explicitly set attributes on a DataArray (e.g., via .assign_attrs()), those attrs were lost because: - With keep_attrs=True: original attrs were copied back, overwriting func's changes - With keep_attrs=False: all attrs were wiped to empty dicts Now, if the function returns a DataArray with non-empty attrs that differ from the original, those attrs are preserved regardless of the keep_attrs setting. This allows users to update variable attributes through Dataset.map, which was previously impossible. Fixes pydata#11356
10ef567 to
6eecafa
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.
Description
This PR fixes #11356 by allowing
Dataset.mapto preserve attributes that are explicitly set by the applied function.Problem
Previously, when a function passed to
Dataset.mapexplicitly set attributes on a DataArray (e.g., via.assign_attrs()), those attrs were lost because:keep_attrs=True: original attrs were copied back, overwriting func's changeskeep_attrs=False: all attrs were wiped to empty dictsThis made it impossible to use
Dataset.mapto update variable attributes.Solution
Now, if the function returns a DataArray with non-empty attrs that differ from the original, those attrs are preserved regardless of the
keep_attrssetting. This allows users to update variable attributes throughDataset.map:The existing behavior is preserved for all other cases:
keep_attrs=Truestill restores original attrs when func doesn't change themkeep_attrs=Falsestill wipes attrs when func doesn't change them.drop_attrs()) still follow thekeep_attrslogicTests Added
test_map_preserves_func_attrscovering all four scenarios:keep_attrs=Truewith func that sets attrs → func's attrs preservedkeep_attrs=Falsewith func that sets attrs → func's attrs preservedkeep_attrs=Truewith func that doesn't set attrs → original attrs restoredkeep_attrs=Falsewith func that doesn't set attrs → attrs wipedFixes #11356