NumPy roll shifts array entries cyclically and wraps them around the selected axis
A cyclic shift moves values to new positions while returning entries that pass an edge to the other end. That boundary rule distinguishes it from inserting empty cells or discarding old observations. Academia Master Trader explains NumPy roll in English for readers in Bosnia and Herzegovina. We will follow a small sequence and a rectangular matrix, keeping the shift and axis explicit. These examples change arrangements only. They do not generate new measurements, forecasts or evidence of financial performance.
Begin with a sequence and one positive step
After import numpy as np, define x = np.array([8,3,6,2]). The call np.roll(x,1) returns [2,8,3,6]. Each entry advances one position along the sequence. Two starts at the last index, passes the right edge and returns at index zero. Eight moves from zero to one, three moves from one to two and six moves from two to three.
The output still has four entries. No zero is inserted at the beginning, and the former last value is not dropped. The wraparound is part of the operation, not a detail to add later. Writing both the original last entry and the new first entry makes the boundary behaviour visible before examining the other positions.
Use a negative shift for the opposite direction
The call np.roll(x,-1) returns [3,6,2,8]. Now each entry moves one position toward smaller indices, with eight returning at the last index. A negative shift changes the direction of movement; it does not make the values negative. The numerical entries remain eight, three, six and two in a new cyclic arrangement.
A shift equal to the sequence length returns the original arrangement. Thus np.roll(x,4) gives [8,3,6,2], and np.roll(x,5) gives the same arrangement as a positive one step shift. The effective movement depends on the shift relative to the axis length. This is a positional property, not an arithmetic change applied to each entry.

Read the figure at the wraparound boundary
The figure displays the original four entry sequence, the positive one step result and the negative one step result. Matching colours track eight and two between the ends. For the positive shift, original two becomes the first entry. For the negative shift, original eight becomes the last entry. Read those edge movements before following the inner entries.
All three displayed sequences contain the same values and have shape (4,). Their sums are equal because no entry was inserted or removed. Equal sums do not identify which shift was used, however. Compare the ordered entries and state the shift. A summary total cannot replace the positional evidence that explains a cyclic arrangement.
Choose an axis for a matrix
Define a = np.array([[8,3,6],[2,9,4]]), with shape (2,3). The call np.roll(a,1,axis=1) returns [[6,8,3],[4,2,9]]. Each row shifts cyclically across its columns. Six wraps from the end of row zero to its beginning, and four wraps within row one. Values remain in their original rows because the chosen axis is the column axis.
The call np.roll(a,1,axis=0) instead returns [[2,9,4],[8,3,6]]. Complete rows change position. There are two rows, so moving one row forward exchanges their places. The order inside each row stays unchanged. Both matrix outputs have shape (2,3), but their values occupy different coordinates because the selected axes differ.
Do not leave the default axis implicit
When axis is omitted, NumPy flattens the input for the shift and then restores the original shape. For our matrix, np.roll(a,1) returns [[4,8,3],[6,2,9]]. This is different from shifting each row separately. Four moves from the final flattened position to the beginning, and six crosses the displayed boundary between the original rows.
That default is useful when the intended operation concerns the flattened sequence, but it should be stated rather than assumed. Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the shape, shift and axis, then follow an entry through the relevant edge. These checks distinguish a cyclic shift from a reversal, a transpose or a shift that pads missing positions.