xoa.coords.transpose

xoa.coords.transpose(da, dims, mode='compat')[source]

Transpose an array

Parameters:
Returns:

xarray.DataArray – Transposed array

Example

In [1]: a = xr.DataArray(np.ones((2, 3, 4)), dims=('y', 'x', 't'))

In [2]: b = xr.DataArray(np.ones((10, 3, 2)), dims=('m', 'y', 'x'))

# classic
In [3]: transpose(a, (Ellipsis, 'y', 'x'), mode='classic')
Out[3]: 
<xarray.DataArray (t: 4, y: 2, x: 3)>
array([[[1., 1., 1.],
        [1., 1., 1.]],

       [[1., 1., 1.],
        [1., 1., 1.]],

       [[1., 1., 1.],
        [1., 1., 1.]],

       [[1., 1., 1.],
        [1., 1., 1.]]])
Dimensions without coordinates: t, y, x

# insert
In [4]: transpose(a, ('m', 'y', 'x', 'z'), mode='insert')
Out[4]: 
<xarray.DataArray (t: 4, m: 1, y: 2, x: 3, z: 1)>
array([[[[[1.],
          [1.],
          [1.]],

         [[1.],
          [1.],
          [1.]]]],



       [[[[1.],
          [1.],
          [1.]],

         [[1.],
          [1.],
          [1.]]]],



       [[[[1.],
          [1.],
          [1.]],

         [[1.],
          [1.],
          [1.]]]],



       [[[[1.],
          [1.],
          [1.]],

         [[1.],
          [1.],
          [1.]]]]])
Dimensions without coordinates: t, m, y, x, z

In [5]: transpose(a, b, mode='insert')
Out[5]: 
<xarray.DataArray (t: 4, m: 1, y: 2, x: 3)>
array([[[[1., 1., 1.],
         [1., 1., 1.]]],


       [[[1., 1., 1.],
         [1., 1., 1.]]],


       [[[1., 1., 1.],
         [1., 1., 1.]]],


       [[[1., 1., 1.],
         [1., 1., 1.]]]])
Dimensions without coordinates: t, m, y, x

# resize
In [6]: transpose(a, b, mode='resize')
Out[6]: 
<xarray.DataArray (t: 4, m: 10, y: 2, x: 3)>
array([[[[1., 1., 1.],
         [1., 1., 1.]],

        [[1., 1., 1.],
         [1., 1., 1.]],

        [[1., 1., 1.],
         [1., 1., 1.]],

        [[1., 1., 1.],
         [1., 1., 1.]],

        [[1., 1., 1.],
         [1., 1., 1.]],

        [[1., 1., 1.],
         [1., 1., 1.]],

        [[1., 1., 1.],
         [1., 1., 1.]],
...
        [[1., 1., 1.],
         [1., 1., 1.]],

        [[1., 1., 1.],
         [1., 1., 1.]],

        [[1., 1., 1.],
         [1., 1., 1.]],

        [[1., 1., 1.],
         [1., 1., 1.]],

        [[1., 1., 1.],
         [1., 1., 1.]],

        [[1., 1., 1.],
         [1., 1., 1.]],

        [[1., 1., 1.],
         [1., 1., 1.]]]])
Dimensions without coordinates: t, m, y, x

In [7]: transpose(a, b.sizes, mode='resize') # with dict
Out[7]: 
<xarray.DataArray (t: 4, m: 10, y: 2, x: 3)>
array([[[[1., 1., 1.],
         [1., 1., 1.]],

        [[1., 1., 1.],
         [1., 1., 1.]],

        [[1., 1., 1.],
         [1., 1., 1.]],

        [[1., 1., 1.],
         [1., 1., 1.]],

        [[1., 1., 1.],
         [1., 1., 1.]],

        [[1., 1., 1.],
         [1., 1., 1.]],

        [[1., 1., 1.],
         [1., 1., 1.]],
...
        [[1., 1., 1.],
         [1., 1., 1.]],

        [[1., 1., 1.],
         [1., 1., 1.]],

        [[1., 1., 1.],
         [1., 1., 1.]],

        [[1., 1., 1.],
         [1., 1., 1.]],

        [[1., 1., 1.],
         [1., 1., 1.]],

        [[1., 1., 1.],
         [1., 1., 1.]],

        [[1., 1., 1.],
         [1., 1., 1.]]]])
Dimensions without coordinates: t, m, y, x

# compat mode
In [8]: transpose(a, ('y', 'x'), mode='compat').dims
Out[8]: ('t', 'y', 'x')

In [9]: transpose(a, b.dims, mode='compat').dims
Out[9]: ('t', 'y', 'x')

In [10]: transpose(a, b, mode='compat').dims  # same as with b.dims
Out[10]: ('t', 'y', 'x')