Skip to content

Improved round tripping for attrs #10275

Open
@hmaarrfk

Description

@hmaarrfk

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions