NumPy roll moves entries cyclically while preserving the array shape
A cyclic shift returns entries to the beginning after they pass the end of a sequence. NumPy roll performs this positional operation along a chosen axis, or across a flattened array when no axis is supplied. Academia Master Trader presents this English lesson for readers in Egypt, using constructed data to distinguish wraparound from padding and from reversing the order.
Start with a vector and one positive shift
After import numpy as np, define v = np.array([3,6,9,12,15]). The call np.roll(v,2) returns [12,15,3,6,9]. Every entry moves two places toward higher indices, with the final two entries wrapping to the beginning.
No zero is inserted and no entry is discarded. The output still has shape (5,). The relative cyclic order remains intact: after fifteen comes three when the sequence is read around its boundary. This differs from sorting or reversing the vector.

A negative shift moves in the opposite direction
The call np.roll(v,-1) returns [6,9,12,15,3]. Three moves past the beginning and reappears at the end. The sign controls the direction, while the magnitude gives the number of positions.
For this five entry vector, shifting by five restores the original arrangement. Shifting by seven gives the same arrangement as shifting by two. Count positions around the sequence rather than treating a large shift as a request to create extra entries or change the output length.
Choose an axis for a matrix
Define m = np.array([[2,4,6],[8,10,12]]). The call np.roll(m,1,axis=1) returns [[6,2,4],[12,8,10]]. Each row wraps its last entry to its own beginning.
Using np.roll(m,1,axis=0) instead returns [[8,10,12],[2,4,6]]. Whole rows move along the first axis, while the order inside each row remains unchanged. Both results have shape (2,3). The same shift value can therefore describe different positional movements depending on the selected axis.
Omitting axis changes how positions are grouped
The call np.roll(m,1) returns [[12,2,4],[6,8,10]]. NumPy first flattens the input, rolls that sequence and then restores the original shape.
Here six crosses the former row boundary, and twelve wraps from the final position to the very beginning. This is not the same as rolling every row independently. Shape alone cannot tell you whether an axis was supplied, because both versions restore or retain the matrix shape. Record the call parameters beside the output.
A useful manual check is to label the positions before moving the values. For the vector, position zero moves to position two under a positive shift of two. Following one position and one boundary crossing makes the direction clear without relying on the numeric size of any entry.
Several axes can have different shifts
The call np.roll(m,shift=(1,1),axis=(0,1)) returns [[12,8,10],[6,2,4]]. The shift tuple pairs each amount with the axis in the corresponding position. One shift moves the rows, and the other moves the columns cyclically.
The operation describes where existing entries go. It does not add values together, interpolate between them or introduce blank space at the edge. Choose another operation when those are the intended effects.
Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. State the shift, axis choice and wraparound rule whenever you explain a rolled array.