Improved round tripping for attrs
Open
Nobody has claimed this yet.
enhancement
- Dominant language
- Python
- Stars
- 4.2k
- Forks
- 1.4k
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 14
Description
Is your feature request related to a problem?
Consider the following dataset:
import xarray as xr
dataset = xr.Dataset()
dataset.attrs = {
'empty_attribute': [],
'attribute_of_length_1': ['one_item_only'],
'attribute_of_length_2': ['one_item', 'two_items'],
}
dataset.to_netcdf('foo.nc')
loaded = xr.open_dataset('foo.nc')
from pprint import pprint
pprint(loaded.attrs)
pprint(loaded.attrs)
{'attribute_of_length_1': 'one_item_only',
'attribute_of_length_2': ['one_item', 'two_items'],
'empty_attribute': array([], dtype=float64)}
- The attrs that contains an empty list has become an array of type float
- The attrs that contains a list of one element, has been downcast to a scalar
- The attrs that contains two elements has been kept as a list of strings.
Describe the solution you'd like
A away to ensure that lists stay lists.
Does this option already exists?
Describe alternatives you've considered
Writing nice error prone if statements
Additional context
I tried to look through https://github.com/pydata/xarray/blob/main/xarray/tests/test_dataset.py and didn't find anything that was testing this special behavior.
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 with the reproducible Dataset.to_netcdf and xr.open_dataset example, then inspect xarray/tests/test_dataset.py, which the issue identifies as a likely test location. Reproduce the empty, one-item, and two-item attribute cases and add coverage for preserving list values through round tripping.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, python
- Domain
- data
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100