pydata / pydata/xarray

Improved round tripping for attrs

Open
#10,275 9 comments 0 reactions 0 assignees View on GitHub

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)}
  1. The attrs that contains an empty list has become an array of type float
  2. The attrs that contains a list of one element, has been downcast to a scalar
  3. 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.