-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Change keep_attrs
default to True
#10726
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?
Conversation
This is indeed a breaking change, which might require a deprecation cycle. Not sure, though, we could also decide that the benefits outweigh the disruption.
should binary operations support (doesn't have to be this PR, though) |
it's difficult to do a deprecation cycle here — we would need to do something like "give a warning anytime we run an operation that drops an attrs" — could be quite noisy. some discussion on this in #3891. not impossible though...
|
BREAKING CHANGE: Change keep_attrs default from False to True This changes the default behavior of xarray operations to preserve attributes by default, which better aligns with user expectations and scientific workflows where metadata preservation is critical. Migration guide: - To restore previous behavior globally: xr.set_options(keep_attrs=False) - To restore for specific operations: use keep_attrs=False parameter - Alternative: use .drop_attrs() method after operations Closes pydata#3891, pydata#4510, pydata#9920
e19c755
to
8453961
Compare
failures in pydata#10726 + `main`
Integrated coordinate preservation feature from main with our keep_attrs changes.
…alse The merge incorrectly preserved coordinate attributes even when keep_attrs=False. Now coordinates have their attrs cleared when keep_attrs=False, consistent with data variables.
- When keep_attrs=True: restore attrs from original coords (func may have dropped them) - When keep_attrs=False: clear all attrs - More efficient than previous implementation
Group attribute operations by keep_attrs value for cleaner, more readable code with identical functionality.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally looks on the right track, though Claude seems to be inserting a lot of specific comments about "now" that will get stale very quickly and should be removed.
keep_attrs = _get_keep_attrs(default=False) | ||
keep_attrs = _get_keep_attrs( | ||
default=True | ||
) # Default now keeps attrs for reduction operations |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment feels like that only makes sense in the context of this PR, not for people reading the code later.
return apply_ufunc(operator.add, a, b, keep_attrs=keep_attrs) | ||
else: | ||
return apply_ufunc(operator.add, a, b) | ||
# Always explicitly pass keep_attrs to test the specific behavior |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
delete
@@ -733,7 +731,7 @@ def add(a, b, keep_attrs): | |||
pytest.param( | |||
None, | |||
[{"a": 1}, {"a": 2}, {"a": 3}], | |||
{}, | |||
{"a": 1}, # apply_ufunc now keeps attrs by default |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also delete
expected0 = indarr[minindex] | ||
expected0.attrs = self.attrs # Default now keeps attrs for reduction operations |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indexing should preserve attrs, right? If so, I'm not sure why this is needed.
as discussed in #3891
Done with lots of help from Claude Code:
This change modifies xarray's default behavior to preserve attributes (
attrs
) across all operations, including computational, binary, and data manipulation functions. Previously, attributes were dropped by default unlesskeep_attrs=True
was explicitly set. This new default aligns xarray with common scientific workflows where metadata preservation is crucial.The
keep_attrs
option now defaults toTrue
for most operations. For binary operations, attributes are preserved from the left-hand operand.This is a breaking change
whats-new.rst
api.rst