NumPy roll shifts entries cyclically while retaining the original array shape
A cyclic rearrangement can move entries without creating or removing a position. NumPy roll returns shifted entries, wrapping any value crossing an end back to the other side. Academia Master Trader presents this English lesson for readers in Côte d'Ivoire, focusing on repeated circuits, reversing a shift and applying one shift amount across several axes.
A short vector reveals the boundary rule
After import numpy as np, define x = np.array([2,4,6,8]). The call np.roll(x,1) returns [8,2,4,6]. The final entry wraps to the first position, and the remaining entries advance toward larger indices.
Shape stays (4,). There is no blank opening position. This boundary rule is suitable only when the end and beginning should connect conceptually.

Large shifts can describe the same arrangement
For this four-entry vector, np.roll(x,9) gives the same ordering as a shift of one: [8,2,4,6]. Nine positions contain two full circuits and one additional position.
The call np.roll(x,8) returns [2,4,6,8], matching the original ordering. Eight positions cover two complete circuits of length four.
The relevant circuit length belongs to the selected axis. A shift amount alone cannot identify whether an output changes: the same amount can be a full circuit on one axis but only part of a circuit on another.
This is a statement about ordering, not a claim that NumPy returns the original Python object when the arrangement repeats.
An opposite shift restores positions
Set shifted = np.roll(x,3). Its entries are [4,6,8,2]. Applying np.roll(shifted,-3) returns [2,4,6,8].
The inverse works because the second operation uses the opposite shift on the same axis. Using another axis in a multidimensional array would describe a different movement, rather than undoing the first one.
A cyclic shift does not estimate new observations. Reversing it restores positions because the operation rearranged existing entries, not because it reconstructed missing information.
One amount can apply across multiple axes
Define grid = np.arange(12).reshape(3,4). Its rows are [0,1,2,3], [4,5,6,7] and [8,9,10,11].
The call np.roll(grid,1,axis=(0,1)) applies the same shift of one to both directions. Its rows become [11,8,9,10], [3,0,1,2] and [7,4,5,6]. Shape remains (3,4).
Here the scalar shift is shared by the listed axes. A tuple of shift amounts instead pairs each amount with its corresponding axis. Keep those two forms distinct when reading code.
For this two-dimensional grid, axis=-1 identifies the final axis, equivalent to axis=1. Omitting axis is different: NumPy shifts a flattened sequence and then restores shape, rather than independently cycling the final direction.
Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the axis length, shift amount and boundary meaning when explaining a cyclic rearrangement.