Skip to content
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

reset_index does not keep attributes #4101

Closed
OriolAbril opened this issue May 27, 2020 · 1 comment · Fixed by #4103
Closed

reset_index does not keep attributes #4101

OriolAbril opened this issue May 27, 2020 · 1 comment · Fixed by #4103
Labels
topic-metadata Relating to the handling of metadata (i.e. attrs and encoding)

Comments

@OriolAbril
Copy link
Contributor

If an indexing coordinate with attributes is converted to a non indexing coordinate with reset_index, the attributes are lost. I am not sure it is a bug, but I think they should keep the attributes and reset_coords does keep the attributes of reset coordinates.

MCVE Code Sample

temp = 15 + 8 * np.random.randn(2, 2, 3)
time = xr.DataArray(pd.date_range('2014-09-06', periods=3), dims=["time"]).assign_attrs({"attr": 23})
coord = xr.DataArray([[-99.83, -99.32], [-99.79, -99.23]], dims=["x", "y"]).assign_attrs({"coord": True})

ds = xr.Dataset(
    {
        'temperature': (['x', 'y', 'time'],  temp),
    },
    coords={
        'coord_0': coord,
        'time': time,
    }
)
ds
# both coord_0 and time have attributes

ds.reset_index("time")
# coordinate time_ does not have attributes anymore

ds.reset_coords("coord_0")
# data variable coord_0 still has attributes

Expected Output

I would expect attributes to be kept.

Possible solution

I was wondering if changing this line and this other line to

vars_to_create[str(d) + "_"] = Variable(d, index, variables[d].attrs)

could solve this. If so I'll send a PR whenever I can.

Versions

Output of xr.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.9 (default, Apr 18 2020, 01:56:04)
[GCC 8.4.0]
python-bits: 64
OS: Linux
OS-release: 4.15.0-101-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
libhdf5: 1.10.4
libnetcdf: 4.6.3

xarray: 0.15.1
pandas: 1.0.3
numpy: 1.18.4
scipy: 1.4.1
netCDF4: 1.5.3
pydap: None
h5netcdf: None
h5py: 2.10.0
Nio: None
zarr: None
cftime: 1.0.4.2
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: 3.2.1
cartopy: None
seaborn: 0.10.1
numbagg: None
setuptools: 42.0.2
pip: 20.1.1
conda: None
pytest: 4.6.2
IPython: 7.14.0
sphinx: 2.0.0

@dcherian
Copy link
Contributor

Sounds good to me. A PR would be welcome.

@dcherian dcherian added the topic-metadata Relating to the handling of metadata (i.e. attrs and encoding) label May 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-metadata Relating to the handling of metadata (i.e. attrs and encoding)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants