NumPy roll shifts positions with wraparound rather than creating empty entries

A shift does not always leave a blank space behind. NumPy roll moves entries along a selected axis and brings entries that pass the end back to the beginning. This circular arrangement preserves the values and the array shape. It is different from filling an exposed position with zero, marking it as missing or deleting an entry.

Academia Master Trader presents this lesson for English-language study in Guyana. The values are invented teaching numbers, not market observations or financial results. A short sequence makes the wraparound visible. A small matrix then shows why naming an axis matters when the same function is applied to more than one dimension.

Start with a sequence of four values

Let x be [3,6,9,12]. The expression np.roll(x,1) produces [12,3,6,9]. Each original entry moves forward by one position, and the final value 12 reappears at the beginning. The result still contains four values, with no new number introduced by the shift.

The number 1 is the shift argument. For this one-dimensional example, a positive shift moves values towards larger position numbers, with wraparound at the boundary. The original value at position 0 appears at position 1. The original final value cannot move beyond the available positions, so it returns to position 0.

Original diagram showing a one-position NumPy roll with the last entry wrapping to the beginning

Read a negative shift separately

The expression np.roll(x,-1) produces [6,9,12,3]. Now the first value reappears at the end. The sign changes the direction of movement along the selected axis. It does not change the values themselves or subtract one from every entry in the sequence.

With four positions, a shift of four gives the original arrangement. A shift of five gives the same arrangement as a shift of one. This follows from wrapping around the same four positions. Describing both the axis length and the shift makes a larger shift easier to interpret without drawing every intermediate step.

Follow a position through the wraparound

For a sequence of length four, an original position i moves to position (i+shift) modulo four. With shift=1, position 3 maps to position 0. That is where the final value 12 appears in the example. Modulo describes the circular boundary rather than a change to the stored value.

This coordinate rule also shows why roll is not a sort. If the input were [9,3,12,6], a shift of one would produce [6,9,3,12]. The function would keep the circular order instead of arranging entries by magnitude. Changing the values does not change the positional mapping.

Name the matrix axis explicitly

Let a have rows [2,4,6] and [8,10,12], with shape (2,3). The call np.roll(a,1,axis=1) shifts the column positions within each row. Its output has rows [6,2,4] and [12,8,10]. Each row wraps independently along its own length-three column axis.

The call np.roll(a,1,axis=0) instead shifts the row positions. Its output has rows [8,10,12] and [2,4,6]. Complete rows move together. The shape remains (2,3) in both cases, but the values occupy different coordinates because the selected axis is different.

Understand what happens when axis is omitted

The default axis is None. The NumPy documentation says the array is flattened before shifting and its original shape is restored afterwards. For the same matrix, np.roll(a,1) gives rows [12,2,4] and [6,8,10]. That differs from shifting each row independently along axis 1.

You can read that default result as a sequence operation: flatten the input to [2,4,6,8,10,12], roll it to [12,2,4,6,8,10], then restore two rows and three columns. Values can therefore cross a displayed row boundary when axis is omitted. The final shape does not tell you which mode was used.

Match multiple shifts to their axes

The documentation allows a tuple of shifts paired with a tuple of axes of the same size. The first shift belongs to the first named axis, the second to the second named axis, and so on. Keeping those pairs together in your explanation prevents a row shift from being described as a column shift.

For this matrix, np.roll(a,(1,1),axis=(0,1)) gives rows [12,8,10] and [6,2,4]. Both the row axis and the column axis have moved by one position. The documentation also allows one integer shift with several named axes, applying that same integer to each of them.

Keep circular shifts distinct from chronological delays

If a sequence represents observations in time order, wraparound has a specific consequence: the last observation can appear at the beginning. That is a property of a circular transformation, not evidence that a later observation was available earlier. The meaning of the data must remain separate from the mechanics of the array operation.

A delay that requires an empty or missing initial position is therefore a different requirement from circular wraparound. State which behaviour your calculation needs before choosing the operation. In this lesson, the output is deliberately circular, so every original teaching value remains present and no missing position is introduced.

Explain the result with its parameters

A clear description names the input shape, shift and axis. For the first example, four values move by one position and the final value wraps to the start. For the matrix, axis 1 shifts columns, axis 0 shifts rows and axis=None treats the entries as one flattened sequence before restoring the shape.

Continue learning with Academia Master Trader at academiamastertrader.com.br. Aprenda opens the educational material. Keep the input beside the shifted output, follow the boundary entry and describe the wraparound before interpreting what the values mean.