bundles / numpy 2.4.4 / numpy / roll
_ArrayFunctionDispatcher
numpy:roll
source: /numpy/_core/numeric.py :1230
Signature
def roll ( a , shift , axis = None ) Summary
Roll array elements along a given axis.
Extended Summary
Elements that roll beyond the last position are re-introduced at the first.
Parameters
a: array_likeInput array.
shift: int or tuple of intsThe number of places by which elements are shifted. If a tuple, then
axismust be a tuple of the same size, and each of the given axes is shifted by the corresponding number. If an int whileaxisis a tuple of ints, then the same value is used for all given axes.axis: int or tuple of ints, optionalAxis or axes along which elements are shifted. By default, the array is flattened before shifting, after which the original shape is restored.
Returns
res: ndarrayOutput array, with the same shape as
a.
Notes
Supports rolling over multiple dimensions simultaneously.
Examples
import numpy as np x = np.arange(10) np.roll(x, 2) np.roll(x, -2)✓
x2 = np.reshape(x, (2, 5)) x2 np.roll(x2, 1) np.roll(x2, -1) np.roll(x2, 1, axis=0) np.roll(x2, -1, axis=0) np.roll(x2, 1, axis=1) np.roll(x2, -1, axis=1) np.roll(x2, (1, 1), axis=(1, 0)) np.roll(x2, (2, 1), axis=(1, 0))✓
See also
- rollaxis
Roll the specified axis backwards, until it lies in a given position.
Aliases
-
numpy.roll