Skip to content

attributes that are lists of strings with a single member don't survive a round-trip #4798

Description

@mikapfl

What happened:

If I create a Dataset with an attr that is a list of strings with only a single member, write it to netcdf and open it again, the attr is a single string instead of a list of strings.

What you expected to happen:

Writing the Dataset to netcdf and opening it should not change the attrs.

Minimal Complete Verifiable Example:

import xarray as xr

ds = xr.Dataset(attrs={"foo": ["bar"]},)

ds.to_netcdf("ds.nc", engine="netcdf4")
rd = xr.open_dataset("ds.nc", engine="netcdf4")

assert ds.attrs["foo"] == rd.attrs["foo"]

Anything else we need to know?:

This was implemented in PR2045, including the test test_setncattr_string. However, there assert_array_equal is used to compare the attrs after a roundtrip (line 1454 ), which unfortunately compares element-wise if one of the elements is a scalar. Changing the
test like this:

-                assert_array_equal(one_element_list_of_strings, totest.attrs["bar"])
+                assert one_element_list_of_strings == totest.attrs["bar"]

corrects it (the test then fails currently).

Environment:

Output of xr.show_versions()

INSTALLED VERSIONS

commit: f52a95c
python: 3.8.6 (default, Sep 25 2020, 09:36:53)
[GCC 10.2.0]
python-bits: 64
OS: Linux
OS-release: 5.8.0-36-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
libhdf5: 1.12.0
libnetcdf: 4.7.4

xarray: 0.16.3.dev75+gf52a95cb
pandas: 1.2.0
numpy: 1.19.5
scipy: None
netCDF4: 1.5.5.1
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: 1.3.0
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: 2020.12.0
distributed: 2020.12.0
matplotlib: None
cartopy: None
seaborn: None
numbagg: None
pint: None
setuptools: 51.1.2
pip: 20.3.3
conda: None
pytest: 6.2.1
IPython: None
sphinx: None

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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