NumPy roll moves elements around a boundary instead of leaving empty positions

A positional shift can mean different things. Some operations leave an empty position at one end, while others bring displaced elements back at the opposite end. NumPy roll uses the second approach. It moves array elements along an axis and wraps elements around the boundary. The output has the same shape as the input.

This behaviour is useful to understand before interpreting a transformed table. A value that appears at the beginning may have come from the end of the original sequence. It is not necessarily a new value, an earlier observation or a replacement for missing data. Its meaning depends on the positional rule that created the output.

Follow one value across the boundary

Start with a one-dimensional array containing 2,4,6,8. A shift of one moves each element one position towards the end. The value 8 goes beyond the last position and is brought back to the first. The result is 8,2,4,6.

np.roll(a, 1)

All four values remain present. The operation does not discard 8 and does not insert a blank or zero at the front. Reading the result as a circular movement explains why the first position contains a value from the original final position.

Original diagram showing a one-position circular shift. Input 2,4,6,8 becomes output 8,2,4,6, with 8 returning at the start.

The diagram identifies the element that crosses the boundary in words. The values alone also show the full result, so the explanation does not depend on tracing a line across labels. Both the input and output contain four positions.

A negative shift moves the other way

For the same input, a shift of minus one gives 4,6,8,2. This time 2 moves beyond the beginning and returns at the end. The direction changes, but the wrapping rule does not. There are still four values and no newly created empty position.

np.roll(a, -1)

A shift of two on this four-element array gives 6,8,2,4. A shift equal to the full length brings the sequence back to its original arrangement. The relevant idea is repeated movement around the same set of positions, not movement onto an indefinitely extending line.

These examples distinguish roll from reversal. Reversing 2,4,6,8 would produce 8,6,4,2. Rolling by one instead produces 8,2,4,6. The first value happens to match, but the remaining order shows that the two operations follow different rules.

Choose the axis for a matrix

Consider a two-row matrix. Its first row is 2,4,6 and its second row is 8,10,12. The shape is (2,3). Rolling by one along axis 1 moves values within each row. The output rows become 6,2,4 and 12,8,10.

np.roll(a, 1, axis=1)

Each row wraps independently along its three column positions. The value 6 returns at the start of the first row, while 12 returns at the start of the second. The operation does not transfer either value into the other row in this version.

Rolling along axis 0 instead moves whole rows through their positions. With two rows and a shift of one, the second row comes first and the first row comes second. The values inside each row stay in their original order. The output remains a two-by-three matrix.

np.roll(a, 1, axis=0)

The same shift number can therefore produce different arrangements. It only becomes a complete positional instruction when combined with an axis or with the function's default handling of the array.

Omitting the axis uses a flattened sequence

By default, roll flattens the array before shifting and then restores the original shape. For our matrix, the flattened order is 2,4,6,8,10,12. Rolling by one gives the flat sequence 12,2,4,6,8,10. Restoring the original shape produces rows 12,2,4 and 6,8,10.

np.roll(a, 1)

This differs from rolling each row independently. The value 6 moves from the last position of the original first row to the first position of the output second row. The value 12 wraps from the end of the flattened sequence to its beginning.

Shape alone cannot reveal that difference, because both results have shape (2,3). Inspect the values as well as the dimensions. An unchanged shape tells you that the structure has been retained, not that every row has been treated as an independent circular sequence.

More than one axis can move

The function accepts tuples for shift and axis. Each shift in the tuple corresponds to the axis in the same position. For example, a shift tuple of (1,2) with an axis tuple of (0,1) requests one position along the first axis and two along the second.

np.roll(a, (1, 2), axis=(0, 1))

The documentation also allows a single integer shift with a tuple of axes. In that case, the same shift is used for every listed axis. Keeping the association explicit is clearer than describing the result as a general rotation of the table.

A clear explanation names the input shape, the shift and the affected axes, then shows enough output values to reveal the wrapping. Where data labels carry meaning, remember that circular movement is a numerical arrangement. The function does not decide whether wrapping the final observation to the beginning is appropriate for the subject being studied.

Academia Master Trader publishes educational material about data organization and numerical methods. Visit academiamastertrader.com.br to explore further topics and continue learning. When reading roll, identify what crosses the boundary and where it returns, rather than assuming a displaced value has disappeared or become missing.

Aprenda