Rolling array positions wraps values around a boundary without creating a time delay

A sequence can be shifted so that each value moves a fixed number of positions. NumPy's roll function performs a circular version of this operation. Values that move beyond one end return at the other end, keeping the array's elements and shape.

This behavior is useful when a circular arrangement is intended. It also needs a clear explanation when the positions represent dates, observations or categories. Moving a value to another position does not change when it was recorded, and wrapping the last value to the beginning does not create an earlier observation.

A one-position circular shift

Consider the four values two, five, eight and one. Applying roll with a positive shift of one produces one, two, five and eight. The last value moves to the first position, and each of the other values moves one position to the right.

A negative shift of one produces five, eight, one and two. The first value moves to the end, while the others move one position to the left. Neither operation sorts the values by size. The movement follows the shift and the positions rather than a ranking.

Circular shift: 2, 5, 8, 1 becomes 1, 2, 5, 8 with shift equal to 1

The array still has four positions after either operation. No empty slot appears at the boundary, and no value is discarded there. This is the defining distinction between circular movement and a shift that leaves a missing value at the newly exposed end.

A wrapped boundary is a choice

The circular rule connects the end of the sequence to its beginning. For a deliberately circular arrangement, that connection can describe the structure being modeled. For an ordinary chronological record, however, the connection is not automatically appropriate.

Suppose the four positions represent four successive days. After a positive circular shift, the value from the fourth day appears in the first position. That placement does not make the fourth day's observation available before the first day. The array operation changes storage positions, not the history of information.

A description should therefore name the circular boundary explicitly. Calling the result a previous-day series without explaining the wrapped element would hide an important difference. A chronological delay may instead need a missing entry or another boundary rule chosen for that purpose.

The axis selects the direction

For a table, the axis parameter determines which positions are moved. Axis zero shifts along row positions, while axis one shifts along column positions within each row. The distinction changes which values cross a boundary together.

Take a table whose first row is one, two, three and whose second row is four, five, six. With shift equal to one and axis equal to zero, the second row moves to the top. The result begins with four, five, six and ends with one, two, three.

With shift equal to one and axis equal to one, each row moves circularly within itself. The first row becomes three, one, two. The second becomes six, four, five. The rows remain in their original order, and each row has its own wraparound boundary.

The default path crosses row boundaries

When axis is not supplied, its default is None. The documentation states that the array is flattened before the shift and then restored to its original shape. This does not mean that each row is shifted independently.

For the same two-row table, a default shift of one produces six, one, two in the first row and three, four, five in the second. The value six wraps to the beginning of the flattened sequence. The value three crosses from the first row into the second.

The output still has two rows and three columns. Shape alone therefore cannot reveal which shift was used. A clear account gives both the shift amount and the axis, or states that the flattened default path was used.

If labels identify the original observations, moving only the numbers may break their association. Whether the labels should move too depends on the purpose of the operation. The distinction should be settled before the result is presented as a labeled series.

For example, rotating a circular display may require the labels and values to travel together. Aligning one sequence against fixed positions is a different operation with a different meaning. In both cases, the boundary rule remains relevant and should not disappear from the explanation.

NumPy also permits shifts on several axes. Corresponding shift amounts and axis choices describe separate movements within the same array. They do not establish that the resulting arrangement is meaningful for the recorded quantities.

The essential reading is simple: roll moves positions and wraps elements around the chosen boundary. It preserves the array's shape, but it does not create observations, establish chronology or supply a missing-value policy. Those decisions belong to the data model and the question being asked.

Academia Master Trader shares educational material about numerical operations, data structures and the interpretation of results at academiamastertrader.com.br.

Aprenda