Austin Benny
Austin Benny

Reputation: 127

xarray change the name of coordinates in dataarray

I feel like i missed something important because the answer to my question seems like it would be easy to find.

Showing a MEW: Let's say i have an xarray,

da = xr.DataArray(
    data=np.ones((10,10,2)),
    dims=['x', 'y', 'orientation'],
    coords={
        "orientation": [1, 2] 
    }
)

It has coordinates 1 and 2 for the dimension orientation. How would i change the coordinates from 1 to one and 2 to two?

The rename method sounds like it can help, and i tried things like

da.rename(orientation={1:'one', 2: 'two'})

or

da.rename({1:'one', 2: 'two'})

to no avail.

Upvotes: 0

Views: 794

Answers (1)

jhamman
jhamman

Reputation: 6444

Xarray's rename method is used to rename keys in the data object (e.g. coordinate names). If you want to replace the values in a coordinate itself, you can simply set those directly:

da['orientation'] = ['one', 'two']
# or using assing_coords
da = da.assign_coords({'orientation': ['one', 'two']})

Upvotes: 0

Related Questions