You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In MetPy we have an attribute managing helper that calls DataArray.assign_attrs() within a callable passed to Dataset.map(). With recent versions of xarray, it is impossible to have the attributes assigned using assign_attrs() to be present in the result. See the following example:
This gives {'units': 'kelvin'}. If instead, keep_attrs=False is used, then we get {}.
This boils down to the fact that there are only two options for a DataArray processed within map():
Get a direct copy (really, reference) to the original DataArray's .attrs
Have .attrs set to {}
There's no option to tell map() to just leave the created DataArray alone. The only work-around I've found is to modify the original DataArray directly instead of using assign_attrs(), which is suboptimal. Things worked fine as recently as xarray 2025.6.1. I know the keep_attrs default changed to True this fall, but I'm surprised setting to False doesn't restore the previous behavior.
for reference, you can use ds.assign({name: var.assign_attrs(...) for name, var in ds.data_vars.items()}).
Not sure how easy it would be to define a clean and consistent API (which would be important), but I'd personally support finding a way of extending Dataset.assign_attrs and DataArray.assign_attrs to support assigning attributes to variables / coordinates.
Activity
keewis commentedon Feb 22, 2026
for reference, you can use
ds.assign({name: var.assign_attrs(...) for name, var in ds.data_vars.items()}).Not sure how easy it would be to define a clean and consistent API (which would be important), but I'd personally support finding a way of extending
Dataset.assign_attrsandDataArray.assign_attrsto support assigning attributes to variables / coordinates.