NumPy roll moves array entries with wraparound rather than inserting empty positions

Moving entries along an array can mean different things at its boundaries. NumPy roll uses circular movement: entries crossing one end return at the other end. Academia Master Trader presents this English lesson for readers in Tunisia, using constructed arrays to explain signed shifts, axis selection and flattened movement that restores the original shape.

A positive shift moves entries towards higher positions

After import numpy as np, define v = np.array([10,20,30,40]). The call np.roll(v,1) returns [40,10,20,30]. The final entry moves to the first position, while the other entries move one position to the right.

The call np.roll(v,-1) returns [20,30,40,10]. A negative shift moves entries towards lower positions, with the first entry wrapping to the end. No input entry disappears and no empty placeholder is inserted.

A shift of four positions returns the original ordering for this four-entry vector. A shift of five has the same ordering as a shift of one. Circular movement repeats after a full circuit of the selected axis.

NumPy circular movement

Choosing an axis determines the boundary that wraps

Define m = np.array([[1,2,3],[4,5,6]]). The call np.roll(m,1,axis=1) returns [[3,1,2],[6,4,5]]. Each row moves across its columns, and the final column wraps to the first column.

The call np.roll(m,1,axis=0) returns [[4,5,6],[1,2,3]]. Entire rows change positions because movement follows the row axis. The entries within each row retain their order.

Both outputs have shape (2,3), matching the input. An axis selects where positions move, not a dimension to remove. Keep the shift and axis together when describing a result, because the same shift can rearrange very different groups of entries.

Omitting the axis uses one flattened sequence

The call np.roll(m,1) returns [[6,1,2],[3,4,5]]. NumPy first treats the matrix as the sequence [1,2,3,4,5,6], rolls that sequence and then restores the original shape.

The entry three therefore crosses the boundary between the first and second output rows. This differs from rolling each row independently. Although the output remains a matrix, omitting axis does not preserve the input rows as separate circular sequences.

Several axes can move in one call

The call np.roll(m,(1,-1),axis=(0,1)) returns [[5,6,4],[2,3,1]]. The first shift applies to axis zero, and the second applies to axis one. Pair each shift with the axis in the corresponding tuple position.

Roll rearranges positions rather than calculating new numeric values. It does not sort entries, reverse their order or add padding. The original array remains available beside the returned result, so the positional changes can be explained directly from the two arrays.

Do not confuse a circular shift with delaying a sequence using a blank starting value. In a delay with padding, a boundary position would need a replacement value. Roll instead supplies an existing entry from the opposite end, which is a different boundary assumption.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the shift, axis and boundary rule before interpreting a circular rearrangement.