Skip to content

reindex drops attrs #6382

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
keewis opened this issue Mar 19, 2022 · 1 comment · Fixed by #6389
Closed

reindex drops attrs #6382

keewis opened this issue Mar 19, 2022 · 1 comment · Fixed by #6389
Labels
bug topic-indexing topic-metadata Relating to the handling of metadata (i.e. attrs and encoding)

Comments

@keewis
Copy link
Collaborator

keewis commented Mar 19, 2022

What happened?

reindex stopped propagating attrs (detected in xarray-contrib/pint-xarray#159).

As far as I can tell, the new reindexing code in Aligner does not handle attrs yet?

Minimal Complete Verifiable Example

# before #5692
In [1]: import xarray as xr
   ...: 
   ...: xr.set_options(keep_attrs=True)
   ...: 
   ...: ds = xr.tutorial.open_dataset("air_temperature")

In [2]: ds.reindex(lat=range(10, 80, 5)).lat
Out[2]: 
<xarray.DataArray 'lat' (lat: 14)>
array([10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75])
Coordinates:
  * lat      (lat) int64 10 15 20 25 30 35 40 45 50 55 60 65 70 75
Attributes:
    standard_name:  latitude
    long_name:      Latitude
    units:          degrees_north
    axis:           Y

In [3]: ds.reindex(lat=xr.DataArray(range(10, 80, 5), attrs={"attr": "value"}, dims="lat")).lat
Out[3]: 
<xarray.DataArray 'lat' (lat: 14)>
array([10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75])
Coordinates:
  * lat      (lat) int64 10 15 20 25 30 35 40 45 50 55 60 65 70 75
Attributes:
    standard_name:  latitude
    long_name:      Latitude
    units:          degrees_north
    axis:           Y

# after #5692
In [3]: import xarray as xr
   ...: 
   ...: xr.set_options(keep_attrs=True)
   ...: 
   ...: ds = xr.tutorial.open_dataset("air_temperature")

In [4]: ds.reindex(lat=range(10, 80, 5)).lat
Out[4]: 
<xarray.DataArray 'lat' (lat: 14)>
array([10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75])
Coordinates:
  * lat      (lat) int64 10 15 20 25 30 35 40 45 50 55 60 65 70 75

In [5]: ds.reindex(lat=xr.DataArray(range(10, 80, 5), attrs={"attr": "value"}, dims="lat")).lat
Out[5]: 
<xarray.DataArray 'lat' (lat: 14)>
array([10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75])
Coordinates:
  * lat      (lat) int64 10 15 20 25 30 35 40 45 50 55 60 65 70 75
@keewis keewis added bug needs triage Issue that has not been reviewed by xarray team member labels Mar 19, 2022
@keewis keewis changed the title reindex treats attributes surprisingly reindex drops attrs Mar 19, 2022
@keewis keewis added topic-indexing topic-metadata Relating to the handling of metadata (i.e. attrs and encoding) and removed needs triage Issue that has not been reviewed by xarray team member labels Mar 19, 2022
@benbovy
Copy link
Member

benbovy commented Mar 20, 2022

Good catch @keewis, #6389 should fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug topic-indexing 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