rearrange

xarray_einstats.einops.rearrange(da, pattern, pattern_in=None, dim_lengths=None, **dim_lengths_kwargs)[source]

Expose einops.rearrange with an xarray-like API.

It has two possible syntaxes which are independent and somewhat complementary.

Wrapper around einops.rearrange with a very similar syntax. Spaces, parenthesis () and -> are not allowed in dimension names.

Parameters:
daxarray.DataArray

Input array

patternstr or list of [hashable, list or dict]

If pattern is a string, it uses the same syntax as einops with two caveats:

  • Unless splitting or stacking, you must use the actual dimension names.

  • When splitting or stacking you can use (dim1 dim2)=dim. This is necessary for the left hand side as it identifies the dimension to split, and optional on the right hand side, if omitted the stacked dimension will be given a default name.

If pattern is not a string, then it must be a list where each of its elements is one of: hashable, list (to stack those dimensions and give them an arbitrary name) or dict (to stack the dimensions indicated as values of the dictionary and name the resulting dimensions with the key).

pattern is then interpreted as the output side of the einops pattern. See About the einops module for more details.

pattern_inlist of [str or dict], optional

The input pattern for the dimensions. It can only be provided if pattern is a list. Also, note this is only necessary if you want to split some dimensions.

The syntax and interpretation is the same as the case when pattern is a list, with the only difference that list elements are not allowed, the same way that (dim1 dim2)=dim is required on the left hand side when using string patterns.

dim_lengths, **dim_lengths_kwargsdict, optional

If the keys are dimensions present in pattern they will be passed to einops.rearrange, otherwise, they are passed to xarray.apply_ufunc.

Returns:
xarray.DataArray