Skip to content

set_index does not respect keep_attrs #4955

Closed
@WardBrian

Description

@WardBrian

What happened: set_index removes attributes from a coordinate, even with xr.set_options(keep_attrs=True)

What you expected to happen: The attributes to be preserved through the coordinate renaming

Minimal Complete Verifiable Example:

import xarray as xr
xr.set_options(keep_attrs=True)
x = xr.DataArray([1,2], dims=['x'])
x.coords['x'] = (['x'],[2,3], {"name":"coord_1"})
x.coords['a'] = (['x'],[0,1], {"name":"coord 2"})
x.set_index(x='a').x.attrs # should be {"name":"coord 2"}, is {}

Environment:

Output of xr.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.8.5 | packaged by conda-forge | (default, Sep 24 2020, 16:55:52)
[GCC 7.5.0]
python-bits: 64
OS: Linux
OS-release: 3.10.0-1160.11.1.el7.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en.UTF-8
LOCALE: None.None
libhdf5: 1.10.6
libnetcdf: 4.7.4

xarray: 0.16.2
pandas: 1.2.0
numpy: 1.19.1
scipy: 1.6.0
netCDF4: 1.5.5.1
pydap: None
h5netcdf: 0.8.1
h5py: 3.1.0
Nio: None
zarr: None
cftime: 1.3.1
nc_time_axis: 1.2.0
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: 1.3.2
dask: 2021.01.0
distributed: 2021.01.0
matplotlib: 3.3.3
cartopy: 0.18.0
seaborn: 0.11.1
numbagg: None
pint: 0.16.1
setuptools: 49.6.0.post20210108
pip: 20.2.3
conda: None
pytest: None
IPython: 7.18.1
sphinx: 3.4.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugtopic-metadataRelating to the handling of metadata (i.e. attrs and encoding)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions