Skip to content

Replacing coord with coord of same name results in NaNs #725

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
spencerahill opened this issue Jan 26, 2016 · 2 comments
Closed

Replacing coord with coord of same name results in NaNs #725

spencerahill opened this issue Jan 26, 2016 · 2 comments
Labels

Comments

@spencerahill
Copy link
Contributor

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!

@shoyer
Copy link
Member

shoyer commented Jan 26, 2016

Indeed, this is definitely a regression. Thanks for the report!

Somehow this does work properly for modifying coordinates on Dataset objects, though:

In [9]: ds = arr.to_dataset(name='foo')

In [10]: ds
Out[10]:
<xarray.Dataset>
Dimensions:  (abc: 3)
Coordinates:
  * abc      (abc) int64 0 1 2
Data variables:
    foo      (abc) int64 0 1 2

In [11]: ds['abc'] = new_coord

In [12]: ds
Out[12]:
<xarray.Dataset>
Dimensions:  (abc: 3)
Coordinates:
  * abc      (abc) int64 1 2 3
Data variables:
    foo      (abc) int64 0 1 2

The logic here is mostly in here: https://github.com/pydata/xarray/blob/v0.7.0/xarray/core/merge.py

There's clearly something missing in the DataArray path (which goes through merge_dataarray_coords)... possibly we need to be using the overwrite_vars argument?

@spencerahill
Copy link
Contributor Author

Thanks, to_dataset is a good workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants