Dataset.map can't update data_var attrs
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the Dataset.map entry point and trace how its keep_attrs argument handles attributes during the mapping operation. Reproduce the reported example with variables carrying units of kilograms, then verify that mapped variables can receive new attributes such as units="unitless" without retaining or wiping them unexpectedly; add or update focused coverage for the selected behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 64/100