Vasco
Vasco

Reputation: 1197

Set coordinates of xarray DataArray dimension

I like to concat/stack different DataArrays into a new one, with certain coordinates at the concat dimension:

x1 = xr.DataArray(np.arange(0,3)[...,np.newaxis], coords=[('b', np.arange(3,6)),('a', [10])]).squeeze()
x2 = xr.DataArray(np.arange(1,4)[...,np.newaxis], coords=[('b', np.arange(3,6)),('a', [10])]).squeeze()

xc = xr.concat([x1,x2], dim='new')
<xarray.DataArray (new: 2, b: 3)>
array([[0, 1, 2],
       [1, 2, 3]])
Coordinates:
  * b        (b) int64 3 4 5
    a        int64 10
Dimensions without coordinates: new

How can I set the coordinates of dimension with the concat function 'new'?

Upvotes: 0

Views: 1333

Answers (2)

jhamman
jhamman

Reputation: 6434

The dim argument in the concat function can take a DataArray or pandas.Index in addition to the name of the concat dimension:

xc = xr.concat([x1,x2], dim=xr.DataArray(['a', 'b'], dims='new', name='new'))

More detail in the xarray.concat documentation: http://xarray.pydata.org/en/stable/generated/xarray.concat.html#xarray.concat

Upvotes: 1

Vasco
Vasco

Reputation: 1197

Setting the coordinates can be done via:

xc['new'] = ['a', 'b']

Upvotes: 1

Related Questions