NumPy roll shifts array elements with wraparound while keeping the original shape
Moving values through an array does not always mean removing the values that reach an edge. NumPy roll shifts elements and brings values that pass the last position back to the first. The output has the same shape as the input. To explain a result clearly, read both the shift and the axis choice. Leaving out the axis has a specific meaning, rather than selecting the first axis automatically.
Academia Master Trader presents this guide in English for readers in Pakistan. The numbers are teaching examples for following positions in arrays. They are not market prices or financial results. The aim is to distinguish a circular shift of elements from a change in the order of the axes, then compare an explicit axis with the default behaviour.
A shift moves values by a number of places
The signature is np.roll(a,shift,axis=None). The first argument is the input array. The shift gives the number of places by which elements move.
The official reference starts with the sequence from zero to nine. A shift of two produces [8,9,0,1,2,3,4,5,6,7].
Eight and nine do not disappear when they move beyond the end. They return at the beginning. This wraparound is the detail that separates the operation from a description that simply says the values move right.

A negative shift changes the direction
For the same input, np.roll(x,-2) produces [2,3,4,5,6,7,8,9,0,1] in the documentation.
The first two values now appear at the end. The sign of the shift is therefore part of the instruction and must remain in any explanation of the call.
Neither result is a numerical sort. The values follow the movement of their positions, with wraparound at the boundary. Their original sequence remains recognisable as a circular arrangement.
A two dimensional input makes the axis choice visible
The reference reshapes the sequence into a table with shape (2,5). Its first row is [0,1,2,3,4] and its second row is [5,6,7,8,9].
This array has axis zero and axis one. Their lengths are two and five respectively.
The axis number is not the shift amount. One identifies the direction within the array structure; the other states how many places the values move. Keeping these roles separate helps explain why two calls with the same shift can produce different arrangements.
Without an axis, flattening comes first
By default, NumPy flattens the input before shifting and restores the original shape afterwards.
For np.roll(x2,1), the reference shows rows [9,0,1,2,3] and [4,5,6,7,8].
The four has crossed the displayed row boundary because the shift was applied to the flattened arrangement. The original shape is restored, but the operation was not a separate shift within each row. This is why omitting the axis cannot be described as choosing axis zero or axis one.
Axis zero shifts the rows in this example
For np.roll(x2,1,axis=0), the output begins with [5,6,7,8,9], followed by [0,1,2,3,4].
The two rows have changed places. Each row keeps its internal sequence of values.
The documentation also shows the same displayed result for a shift of negative one along axis zero in this particular two-row array. Do not turn that local coincidence into a general rule that positive and negative shifts always agree. Read it with the length of this axis in mind.
Axis one shifts within each row
For np.roll(x2,1,axis=1), the rows become [4,0,1,2,3] and [9,5,6,7,8].
Each row wraps independently along axis one. The final value of the first row returns to the start of that row, rather than moving into the other row.
Compare this with the flattened default result. Both calls preserve shape (2,5), but the values occupy different positions. Shape alone does not tell you which axis behaviour was used.
Several axes can have corresponding shifts
The shift and axis parameters can both be tuples. The reference pairs shifts (2,1) with axes (1,0).
This assigns a shift of two to axis one and a shift of one to axis zero. The documented rows are [8,9,5,6,7] and [3,4,0,1,2].
Preserve the pairing order when describing such an instruction. A tuple of shifts is not a list of desired output lengths, and roll does not move the axes themselves to new positions.
Continue learning with Academia Master Trader at academiamastertrader.com.br. Aprenda gives access to educational resources. Read the signed shift, identify whether an axis is supplied, and follow a few values across the boundary. The output keeps the original shape, while the chosen shifting rule determines where the elements appear.