scipp.transpose#

scipp.transpose(x, dims=None)#

Transpose dimensions of the input.

Parameters:
Returns:

Same type as input – The transpose of the input.

Raises:

scipp.DimensionError – If dims are incompatible with the input data.

Examples

Reverse the dimensions of a 2-D array:

>>> import scipp as sc
>>> data = sc.array(dims=['x', 'y'], values=[[1, 2, 3], [4, 5, 6]])
>>> data
<scipp.Variable> (x: 2, y: 3)      int64  [dimensionless]  [1, 2, ..., 5, 6]
>>> data.values
array([[1, 2, 3],
       [4, 5, 6]])
>>> sc.transpose(data)
<scipp.Variable> (y: 3, x: 2)      int64  [dimensionless]  [1, 4, ..., 3, 6]
>>> sc.transpose(data).values
array([[1, 4],
       [2, 5],
       [3, 6]])

Specify the order of dimensions explicitly:

>>> data3d = sc.array(dims=['x', 'y', 'z'], values=sc.arange('t', 24).values.reshape(2, 3, 4))
>>> data3d.sizes
{'x': 2, 'y': 3, 'z': 4}
>>> sc.transpose(data3d, dims=['z', 'x', 'y']).sizes
{'z': 4, 'x': 2, 'y': 3}

With a DataArray, coordinates are preserved:

>>> da = sc.DataArray(
...     data=sc.array(dims=['x', 'y'], values=[[1, 2], [3, 4]], unit='K'),
...     coords={
...         'x': sc.array(dims=['x'], values=[0.0, 1.0], unit='m'),
...         'y': sc.array(dims=['y'], values=[10, 20], unit='s')
...     }
... )
>>> sc.transpose(da)
<scipp.DataArray>
Dimensions: Sizes[y:2, x:2, ]
Coordinates:
* x                         float64              [m]  (x)  [0, 1]
* y                           int64              [s]  (y)  [10, 20]
Data:
                              int64              [K]  (y, x)  [1, 3, 2, 4]