Modifying Coordinate Variables

2,429 views
Skip to first unread message

erol512

unread,
Aug 8, 2016, 8:01:39 PM8/8/16
to xar...@googlegroups.com
This seems like it should be a simple operation. Does someone know the proper way to modify a coordinate variable in a dataset that is being used as an index? I have a dataset with an invalid vertical axis that I want to correct and re-save.

Cheers,
Jeremy

Stephan Hoyer

unread,
Aug 9, 2016, 1:34:59 AM8/9/16
to xarray
With an index, coordinate values are immutable because they are backed by a pandas.Index (which has immutable values).

So it's most straightforward to replace a coordinate variable wholesale with something else, e.g.,

In [31]: ds = xr.Dataset(coords={'x': range(3)})

In [32]: ds['x'] = [5, 6, 7]

In [33]: ds
Out[33]:
<xarray.Dataset>
Dimensions:  (x: 3)
Coordinates:
  * x        (x) int64 5 6 7
Data variables:
    *empty*

In [36]: ds['x'] = 2 * ds['x']

In [37]: ds
Out[37]:
<xarray.Dataset>
Dimensions:  (x: 3)
Coordinates:
  * x        (x) float64 10.0 12.0 14.0
Data variables:
    *empty*

You can modify values with indexing if you convert to an ndarray and then reconstruct the coordinate. In principle, it should also work to modify a copy of a DataArray created from an index (e.g., ds['x'].copy()), but currently that remains immutable:

--
You received this message because you are subscribed to the Google Groups "xarray" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xarray+unsubscribe@googlegroups.com.
To post to this group, send email to xar...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/xarray/CAOTezYV5EuLg-jJvrDvH2QLyvCnY6bvsPSBhJ8kv0fdL8jRBVQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

erol512

unread,
Aug 9, 2016, 3:49:59 PM8/9/16
to xar...@googlegroups.com
Thanks! I was missing the fact that you could replace coordinate variables by assignment.

Jeremy

Reply all
Reply to author
Forward
0 new messages