larray.Array.roll

Array.roll(axis=None, n=1) Array[source]

Roll the cells of the array n-times to the right along axis. Cells which would be pushed “outside of the axis” are reintroduced on the opposite side of the axis.

Parameters
axisint, str or Axis, optional

Axis along which to roll. Defaults to None (all axes).

nint or Array, optional

Number of positions to roll. Defaults to 1. Use a negative integers to roll left. If n is an Array the number of positions rolled can vary along the axes of n.

Returns
Array

See also

Array.shift

cells which are pushed “outside of the axis” are dropped instead of being reintroduced on the opposite side of the axis.

Examples

>>> arr = ndtest('sex=M,F;year=2019..2021')
>>> arr
sex\year  2019  2020  2021
       M     0     1     2
       F     3     4     5
>>> arr.roll('year')
sex\year  2019  2020  2021
       M     2     0     1
       F     5     3     4

One can also roll by a different amount depending on another axis

>>> # let us roll by 1 for men and by 2 for women
>>> n = sequence(arr.sex, initial=1)
>>> n
sex  M  F
     1  2
>>> arr.roll('year', n)
sex\year  2019  2020  2021
       M     2     0     1
       F     4     5     3