best way to replace a dimension

1,621 views
Skip to first unread message

Ryan Abernathey

unread,
Feb 16, 2017, 6:09:46 PM2/16/17
to xar...@googlegroups.com
Hi Folks,

I need a function that takes a dataarray and replaces a dimension with a coordinate from a different dataset. I would like some advice on the best way to do this.

Performance is important, and checking is not. I want to bypass all reindexing, aligning, etc and operate at a low level.

Here's what I have now. I'm sure there is a better way.

Thanks,
Ryan

def _replace_dim(da, olddim, newdim):
    """Replace a dimension with a new dimension

    PARAMETERS
    ----------
    da : xarray.DataArray
    olddim : str
        name of the dimension to replace
    newdim : xarray.DataArray
        dimension to replace it with

    RETURNS
    -------
    da_new : xarray.DataArray
    """

    # get a dictionary of coords
    # can't use da.coords because it is an immutable
    # xarray.core.coordinates.DataArrayCoordinates object
    coords = {dim: da[dim] for dim in da.dims}
    # add the new dimension to it
    coords[newdim.name] = newdim
    # delete the old dimension
    del coords[olddim]

    # redefine dims
    dims = list(da.dims)
    dims[dims.index(olddim)] = newdim.name

    # define a new dataset
    return xr.DataArray(da.data, coords, dims)

Joe Hamman

unread,
Feb 16, 2017, 8:16:12 PM2/16/17
to xarray
Ryan, I'm wondering if something like this would work:

new = da.rename({olddim, newdim.name})
new.coords[newdim.name] = newdim

Joe

Stephan Hoyer

unread,
Feb 16, 2017, 8:16:51 PM2/16/17
to xarray
It's still pretty awkward, but I think something like the following would work:

def _replace_dim(da, olddim, newdim):
    renamed = da.rename({olddim: newdim.name})

    # note that alignment along a dimension is skipped when you are overriding
    # the relevant coordinate values
    renamed.coords[newdim.name] = newdim
    return renamed

One advantage of this version is that the "rename" operation ensures that the dimension gets swapped out on every coordinate that might be using it, too (in your version these would raise an error).

This comes up with some regularity, so possibly it's worth adding a built-in method to take care of this.


--
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/CAJAZx5A7M_2w8bfnA%3DXbnWwj3PKo1tB6cybycn%2B6gas0gpg5ZA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Ryan Abernathey

unread,
Feb 16, 2017, 9:13:34 PM2/16/17
to xar...@googlegroups.com
Thanks for the advice. This is much better, because it also keep the auxiliary coordinates in place.

Since there might be broader interest, I will open an issue.

Ryan Abernathey

unread,
Feb 16, 2017, 9:23:12 PM2/16/17
to xar...@googlegroups.com
Wow, incredible agreement in the solutions proposed by Joe and Stephan!
Reply all
Reply to author
Forward
0 new messages