0

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'?

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

Setting the coordinates can be done via:

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

2 Comments

This needs two line of codes, can it be done in one?
Sometimes the solution with more lines of code is easier to read. I am much more likely to remember this solution than the more complex one-liner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.