Skip to content

Conversation

@keewis
Copy link
Collaborator

@keewis keewis commented Aug 4, 2025

This makes sure we propagate attrs in coords in Dataset.map if keep_attrs == True. It uses the same semantics as for the data variables, which means that keep_attrs=True will overwrite any changes to the attrs by the mapped function.

This hides the use of the division operator in some of the weighted operations, which are solely affected by the global keep_attrs property.

Alternative implementation options:

  • Instead of collecting and merging the coords then copying over coordinate attrs we could also make DataArray._copy_attrs_from iterate over the coordinates.
  • Instead of modifying Dataset.map, we could also make all weighted ops receive a keep_attrs parameter

@keewis keewis closed this Aug 12, 2025
@keewis keewis reopened this Aug 12, 2025
@dcherian
Copy link
Contributor

Instead of modifying Dataset.map, we could also make all weighted ops receive a keep_attrs parameter

I think this is a good improvement for Dataset.map. I don't think weighted should take keep_attrs, we've tried to leave that control to the actual reduction (.weighted.mean) : #4513

@max-sixty
Copy link
Collaborator

agree! can we merge and then I'll merge into #10726

@keewis
Copy link
Collaborator Author

keewis commented Sep 10, 2025

If I can count your comments as two approvals, then that should be enough to merge once CI passes

@max-sixty max-sixty enabled auto-merge (squash) September 10, 2025 18:05
@max-sixty max-sixty merged commit 5d9aa6e into pydata:main Sep 10, 2025
36 checks passed
shoyer pushed a commit to shoyer/xarray that referenced this pull request Sep 16, 2025
* check that weighted ops propagate attrs on coords

* propagate attrs on coords in `map` if keep_attrs

* directly check that `map` propagates attrs on coords

* whats-new
max-sixty added a commit to max-sixty/xarray that referenced this pull request Oct 9, 2025
After PR pydata#10602, Dataset.map started failing when functions returned
non-DataArray values (e.g., scalars), raising AttributeError when
trying to access .coords on the returned values.

This restores backward compatibility by converting non-DataArray
outputs to DataArrays, which was the behavior before the regression.

Fixes pydata#10835

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
max-sixty added a commit that referenced this pull request Oct 10, 2025
After PR #10602, Dataset.map started failing when functions returned
non-DataArray values (e.g., scalars), raising AttributeError when
trying to access .coords on the returned values.

This restores backward compatibility by converting non-DataArray
outputs to DataArrays, which was the behavior before the regression.

Fixes #10835

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing coordinate attributes after weighted mean

3 participants