-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Refactor Dataset.map to merge attrs instead of copying #11020
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
8df355e
b558513
d243e16
89010a8
f9a5669
6645853
966b49f
05f42d7
ec5d6fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6908,8 +6908,10 @@ def map( | |
| DataArray. | ||
| keep_attrs : bool or None, optional | ||
| If True, both the dataset's and variables' attributes (`attrs`) will be | ||
| copied from the original objects to the new ones. If False, the new dataset | ||
| and variables will be returned without copying the attributes. | ||
| combined from the original objects and the function results using the | ||
| ``drop_conflicts`` strategy: matching attrs are kept, conflicting attrs | ||
| are dropped. If False, the new dataset and variables will have only | ||
| the attributes set by the function. | ||
| args : iterable, optional | ||
| Positional arguments passed on to `func`. | ||
| **kwargs : Any | ||
|
|
@@ -6958,16 +6960,19 @@ def map( | |
| coords = Coordinates._construct_direct(coords=coord_vars, indexes=indexes) | ||
|
|
||
| if keep_attrs: | ||
| # Merge attrs from function result and original, dropping conflicts | ||
| from xarray.structure.merge import merge_attrs | ||
|
|
||
| for k, v in variables.items(): | ||
| v._copy_attrs_from(self.data_vars[k]) | ||
| v.attrs = merge_attrs( | ||
| [v.attrs, self.data_vars[k].attrs], "drop_conflicts" | ||
| ) | ||
| for k, v in coords.items(): | ||
| if k in self.coords: | ||
| v._copy_attrs_from(self.coords[k]) | ||
| else: | ||
| for v in variables.values(): | ||
| v.attrs = {} | ||
| for v in coords.values(): | ||
| v.attrs = {} | ||
| v.attrs = merge_attrs( | ||
| [v.attrs, self.coords[k].attrs], "drop_conflicts" | ||
| ) | ||
| # When keep_attrs=False, leave attrs as the function returned them | ||
|
Comment on lines
6960
to
+6975
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem with interpreting I'd argue that So instead we may need to consider supporting
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, the proposed code treats @keewis can you see a reasonable change to fix the immediate issue without adding a whole strategy to
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (zooming out — as I mentioned before, for me the best "blank-slate" implementation for
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It seems like it wouldn't be too hard to make this more configurable by just passing any string that is passed to |
||
|
|
||
| attrs = self.attrs if keep_attrs else None | ||
jsignell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return type(self)(variables, coords=coords, attrs=attrs) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.