Repository navigation
Open
Description
What is your issue?
Since #10726 (v2025.11.0) Dataset.map has no way to keep attributes set by the mapped function: keep_attrs=True overwrites them with the original attrs, keep_attrs=False drops them. #11020 fixes this regression by leaving the function's attrs alone for keep_attrs=False and merging with drop_conflicts for keep_attrs=True. The discussion there showed that the boolean keep_attrs is the wrong interface for map:
- The function receives xarray objects, so it can handle attrs itself.
keep_attrsmainly gets in the way of that (@dcherian, @max-sixty). - Coordinates can still be modified inside the function, so the coordinate attrs of the results need to be combined somehow. That is what the argument should control instead, e.g. as
combine_attrs(@jsignell, @dcherian). - A boolean
keep_attrsis ambiguous: doesFalsemean "drop the input attrs" or "drop all attrs on the output"? (@keewis, @jsignell; see also Change behavior ofkeep_attrsinxr.wherewhen x is a scalar #10997) mapis basically a loop over.data_vars, and users who need full control can do that loop themselves, e.g.ds.assign({name: func(var) for name, var in ds.data_vars.items()})orxr.merge(..., combine_attrs=...)(@keewis, @jsignell).- Before deprecating
keep_attrs, users need a way to opt into the new behavior (@keewis, @jsignell).
Options discussed for that:
- Add
combine_attrstomap, accepting the usual strategy names / a callable, and deprecatekeep_attrs. This is the option @keewis and @jsignell leaned towards. - Let
keep_attrsaccept strategy names / a callable, likeapply_ufunc, mappingTrueto"override"andFalseto"drop". This avoids a new argument, but a single value can't then control data variable and coordinate attrs separately. - Remove
keep_attrsfrommapwithout replacement and keep whatever attrs the function returns.
Activity