Skip to content

set_index does not respect keep_attrs #4955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
WardBrian opened this issue Feb 25, 2021 · 2 comments · Fixed by #5692
Closed

set_index does not respect keep_attrs #4955

WardBrian opened this issue Feb 25, 2021 · 2 comments · Fixed by #5692
Labels
bug topic-metadata Relating to the handling of metadata (i.e. attrs and encoding)

Comments

@WardBrian
Copy link
Contributor

WardBrian commented Feb 25, 2021

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

@mathause
Copy link
Collaborator

Did you mean? (Notice the x.x)

x = x.set_index(x='a')
x.x.attrs

I am marking this as a bug.

@mathause mathause added bug topic-metadata Relating to the handling of metadata (i.e. attrs and encoding) labels Feb 25, 2021
@WardBrian
Copy link
Contributor Author

Did you mean? (Notice the x.x)

Yes, my bad

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug topic-metadata Relating to the handling of metadata (i.e. attrs and encoding)
Projects
None yet
2 participants