Skip to content

Dataset.map can't update data_var attrs #11356

@91Seconds

Description

@91Seconds

What is your issue?

I was trying to use dataset.map to update the attributes of every data variable in my dataset. It looks like there's no way to do this since passing keep_attrs=True retains the previous attributes while passing keep_attrs=False wipes the attributes. Maybe keep_attrs should be one of ["source", "destination", "wipe"] rather than being a boolean.

My motivating usecase involves a dataset with variables with units of kilograms. Each data variable has attr dict {"units": "kg"}. I then run ds.map(lambda x: x / x.sum()) to get a dataset of unitless mass distributions, each of which I'd like to have an attr dict of {"units": "unitless"}.

Unfortunately, running

ds.map(lambda x: (x / x.sum()).assign_attrs(units="unitless"), keep_attrs=keep_attrs)

either results in the original attr dict or an empty attr dict depending on whether keep_attrs is True or False

I think the inability to use .map to update variable attributes is a legitimate missing capability in an otherwise very well-considered API. But... if there's a deep reason that it needs to be this way then I'd also love to know why.

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs triageIssue that has not been reviewed by xarray team member

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions