Description
I think this is another unintended consequence of #648. Consider the following case:
In [20]: arr = xr.DataArray(range(3), dims=['abc'])
In [21]: new_coord = xr.DataArray([1,2,3], dims=['abc'], coords=[[1,2,3]])
In [22]: arr['abc'] = new_coord
In [23]: arr
Out[24]:
<xarray.DataArray (abc: 3)>
array([0, 1, 2])
Coordinates:
* abc (abc) float64 nan 1.0 2.0
Note the nan
. Before #648, this worked, in that arr
's coordinates would consist of new_coord
.
The use case: we have some data defined on the edges of pressure levels in an atmospheric model, and other data defined at the center of the pressure levels. In order to perform calculations involving both kinds of data, we average the edge-defined data (i.e. 0.5*(value at top edge + value at bottom edge)) to get the value at the level centers. But the resulting DataArray still has as its coord (from xarray's perspective, that is) the level edges, and so we replace that coord with the DataArray of the level centers.
A workaround would be arr['abc'].values = new_coord.values
, but then any other metadata associated with the original coord is retained, which is not good.
Somewhat involved and not sure I described clearly, so let me know if clarification needed. Also I vaguely suspect there's a cleaner way of doing this in the first place. Thanks!