NumPy roll shifts array entries with wraparound while keeping their original shape

A circular shift moves entries through an array and brings the entries that cross one end back at the other end. NumPy roll performs that wraparound rearrangement. Academia Master Trader presents this English lesson for readers in Indonesia. Constructed examples explain positive and negative shifts, matrix axes and the default flattened path, without confusing wraparound with a time lag that introduces empty positions.

Move entries by a stated number of places

After import numpy as np, define a = np.array([2,5,8,11]). The call np.roll(a,1) returns [11,2,5,8]. Each entry moves one position toward a higher index, and eleven wraps from the final position to the first. The four entry shape (4,) is preserved.

The call np.roll(a,2) returns [8,11,2,5]. Two entries wrap around rather than disappearing. No new zero or missing value is inserted. The output contains the original entries in a circularly shifted arrangement, not their arithmetic differences or a new set of measured values.

NumPy roll

A negative shift moves the opposite way

The call np.roll(a,-1) returns [5,8,11,2]. Five moves into the first position, and two wraps from the beginning to the end. The sign of the shift controls the direction of movement; it does not negate the input values.

For this four entry vector, a shift of four returns the same arrangement as the input. A shift of five gives the same arrangement as a shift of one. Circular movement repeats after a complete turn through the selected length. Record the shift and the axis length when explaining equivalent arrangements.

An explicit axis keeps matrix boundaries clear

Define m = np.array([[1,4,7],[2,5,8]]). The call np.roll(m,1,axis=0) returns [[2,5,8],[1,4,7]]. Each row moves one row position, with the final row returning at the beginning. The entries inside each row keep their order.

The call np.roll(m,1,axis=1) returns [[7,1,4],[8,2,5]]. Each row is shifted across its columns, and its last entry returns at the start of that same row. The matrix retains shape (2,3), but the two axis choices describe different rearrangements.

Name the data meaning of each axis before choosing one. A row might represent a category, an observation or something else entirely. Roll does not inspect those meanings and does not automatically move separately stored labels with the numeric entries.

The default uses a flattened path

The call np.roll(m,1) returns [[8,1,4],[7,2,5]]. Without an axis argument, NumPy flattens the array before shifting and restores the original shape afterward. The shape remains (2,3), but entries can cross the old row boundary during the flattened movement.

The final source entry, eight, becomes the first output entry. Seven crosses from the end of the first source row into the beginning of the second output row. This is different from shifting each row independently. An unchanged matrix shape does not prove that its original row groups were kept together.

Wraparound is a choice about the boundary

A circular shift is appropriate only when that boundary behavior matches the intended calculation. If the goal is a previous observation column with an empty first position, roll alone does not supply it. Its first output can contain the final observation, which came from the other end of the sequence.

Keep the transformation distinct from any interpretation of cause, prediction or chronological availability. Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. State the shift, selected axis and wraparound rule so every relocated entry can be traced to its source position.