Skip to content

reindex drops attrs #6382

Closed
Closed
@keewis

Description

@keewis

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugtopic-indexingtopic-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