How to Shift Elements in NumPy Array (With Examples)

How to Easily Shift Elements in a NumPy Array

Manipulating the position of data elements is a fundamental requirement in data processing and analysis. When working with large datasets in Python, the NumPy library is indispensable for its high performance and optimized array operations. Shifting elements within a NumPy array involves changing the positional index of the values. This operation is crucial in applications like signal processing, data alignment, and time-series analysis where relative positioning matters significantly.

NumPy provides powerful, built-in tools to handle array shifts, most notably the np.roll() function. However, the nature of shifting can be categorized into two main types: cyclic shifting (where elements wrap around) and linear shifting (where shifted elements are replaced by a specified fill value). Understanding the distinction between these two approaches is key to performing accurate array manipulation.

In this expert guide, we will thoroughly explore both methods. We begin with the efficient, native np.roll() function for cyclic shifts, which maintains all original elements within the array structure. Subsequently, we will demonstrate how to construct a robust, custom function to perform linear shifts, allowing you to control element replacement using padding values like zeros or NaNs.


To effectively shift elements in a NumPy array, you typically rely on one of the following two established methodologies, depending on whether you need the data to cycle or be padded:

Method 1: Cyclic Shifting (Keeping All Original Elements)

# Shift each element two positions to the right, causing wrap-around.
data_new = np.roll(data, 2)

Method 2: Linear Shifting (Allowing Elements to Be Replaced/Padded)

# Define shifting function using custom logic for padding
def shift_elements(arr, num, fill_value):
    result = np.empty_like(arr)
    if num > 0:
        result[:num] = fill_value
        result[num:] = arr[:-num]
    elif num < 0:
        result[num:] = fill_value
        result[:num] = arr[-num:]
    else:
        result[:] = arr
    return result

# Shift each element two positions to the right (replace shifted elements with zero)
data_new = shift_elements(data, 2, 0)

The subsequent sections provide comprehensive examples demonstrating how to implement and interpret the results of each method in practice, ensuring you can choose the correct technique for your data requirements.

Method 1: Implementing Cyclic Shifts with np.roll()

The standard method for shifting elements while maintaining the integrity and size of the original data structure is through NumPy’s roll() function. The np.roll() function performs what is known as a cyclic shift, or circular shift. When elements are moved off one end of the array, they automatically reappear on the opposite end. This behavior is fundamentally different from a linear shift, where elements might be discarded or replaced by padding.

The primary benefit of using np.roll() is its optimization. As a native NumPy function written in C, it executes shifts extremely quickly, especially on very large arrays. It accepts two main arguments: the array to be shifted and the number of positions (the shift count). A positive shift count moves elements towards the end of the array (rightward shift), causing the elements at the end to wrap back to the beginning. Conversely, a negative shift count moves elements towards the beginning (leftward shift).

It is important to note that np.roll() operates on the entire array axis by default, but it can also be constrained to specific dimensions when dealing with multi-dimensional arrays, making it incredibly versatile for tasks involving image processing or complex matrix rotations. We will focus here on one-dimensional arrays for clarity, demonstrating both positive and negative shifts.

Example 1: Performing a Rightward Cyclic Shift

To execute a rightward shift, we pass a positive integer to the shift parameter of the np.roll() function. If we specify a shift of 2, every element moves two positions to the right, and the last two elements wrap around to occupy the first two positions, ensuring no data loss occurs.

Consider an initial array [1, 2, 3, 4, 5, 6]. A right shift of two positions moves 1 to index 2, 2 to index 3, and so on. The values 5 and 6, which are shifted out of bounds, return to the start, resulting in the new array [5, 6, 1, 2, 3, 4].

The following implementation illustrates this concept clearly, importing the necessary NumPy library and defining the array before applying the cyclic operation. The output confirms the wrap-around behavior characteristic of np.roll().

import numpy as np

#create NumPy array
data = np.array([1, 2, 3, 4, 5, 6])

#shift each element two positions to the right
data_new = np.roll(data, 2)

#view new NumPy array
data_new

array([5, 6, 1, 2, 3, 4])

Notice that each element was shifted two positions to the right and elements at the end of the array simply got moved to the front.

Example 2: Performing a Leftward Cyclic Shift

To shift elements cyclically towards the beginning of the array (a leftward shift), we simply provide a negative integer value to the shift parameter of the np.roll() function. The magnitude of the negative number dictates how many positions each element is shifted. Elements that are shifted off the left end of the array will wrap around and appear at the right end.

If we use the same starting array, [1, 2, 3, 4, 5, 6], and apply a left shift of three positions (-3), the first three elements (1, 2, and 3) move to the end of the array, while 4, 5, and 6 move to the beginning. The resultant structure is [4, 5, 6, 1, 2, 3]. This mechanism is mathematically equivalent to rotating the array.

This technique is exceptionally useful when processing data streams or performing time-series analysis where the relative chronological order of data points is maintained, but the reference point (start of the window) needs to move. The code below demonstrates how to achieve this leftward rotation using the negative shift argument.

import numpy as np

#create NumPy array
data = np.array([1, 2, 3, 4, 5, 6])

#shift each element three positions to the left
data_new = np.roll(data, -3)

#view new NumPy array
data_new

array([4, 5, 6, 1, 2, 3])

Method 2: Implementing Linear Shifts with Custom Functions

While cyclic shifting (np.roll()) is excellent for rotational tasks, many real-world applications, especially those involving padding or boundary conditions, require a linear shift. A linear shift moves elements a specified number of positions, but instead of wrapping the displaced elements around, it fills the newly vacated positions with a predefined constant value (the fill value), such as zero, NaN, or the mean of the dataset. This approach effectively aligns the data within the array while introducing placeholder values where data was shifted out.

Since NumPy does not offer a native built-in function for linear shifting with padding as straightforwardly as np.roll(), we must define a custom function. This custom function allows granular control over the fill value and ensures that the array remains the same size (preserving its shape) while accommodating the linear translation of data.

Our custom function, which we name shift_elements, must handle three scenarios: a positive shift (right), a negative shift (left), and a zero shift (no change). The core logic relies on slicing the original array and combining it with slices containing the desired fill value, carefully ensuring the correct portion of the array is preserved and the correct boundary is padded.

Implementing the Custom Shift Function

The custom function shift_elements(arr, num, fill_value) takes the original array, the shift count (num), and the padding value as arguments. The first critical step is allocating space for the result using np.empty_like(arr). This ensures the output array is initialized with the correct dimensions and data type as the input array, improving efficiency.

For a positive shift (rightward movement, num > 0), the first num elements of the new array must be filled with the fill_value, effectively acting as the padding zone. The remaining elements are populated by the original array, excluding the last num elements that have been shifted off the right side (arr[:-num]).

Conversely, for a negative shift (leftward movement, num < 0), the padding must occur at the end of the array. The last |num| elements of the result are set to the fill_value, and the beginning elements are filled with the original array, starting from the |num|-th element (arr[-num:]). This systematic approach ensures accurate linear shifting for any input array and shift count.

def shift_elements(arr, num, fill_value):
    result = np.empty_like(arr)
    if num > 0:
        result[:num] = fill_value
        result[num:] = arr[:-num]
    elif num < 0:
        result[num:] = fill_value
        result[:num] = arr[-num:]
    else:
        result[:] = arr
    return result

Example 3: Rightward Linear Shift (Replacing Elements)

Using our defined custom function, we can now execute a linear shift where the displaced elements are replaced. In this example, we shift the array two positions to the right and use 0 as the fill_value.

Starting with [1, 2, 3, 4, 5, 6], shifting two positions to the right means that 5 and 6 are conceptually pushed out. The first two positions are then filled by 0 (the specified padding value). The elements 1, 2, 3, 4 occupy the remaining positions, resulting in [0, 0, 1, 2, 3, 4].

This scenario is typical when dealing with sensor data or time windows where missing data points (or points shifted out of the current window) must be represented by a neutral value. The code below demonstrates the initialization, function definition, and execution of this rightward linear shift.

import numpy as np

#create NumPy array
data = np.array([1, 2, 3, 4, 5, 6])

#define custom function to shift elements
def shift_elements(arr, num, fill_value):
    result = np.empty_like(arr)
    if num > 0:
        result[:num] = fill_value
        result[num:] = arr[:-num]
    elif num < 0:
        result[num:] = fill_value
        result[:num] = arr[-num:]
    else:
        result[:] = arr
    return result

#shift each element two positions to the right and replace shifted values with zero
data_new = shift_elements(data, 2, 0)

#view new NumPy array
data_new

array([0, 0, 1, 2, 3, 4])

Example 4: Leftward Linear Shift (Replacing Elements)

To demonstrate the flexibility of the custom function, we can perform a leftward linear shift by using a negative shift count. In this scenario, elements are pushed out from the beginning of the array, and the new padding values are introduced at the end.

Here, we shift the array three positions to the left (-3) and use the value 50 as the replacement value, showing that the fill value is not limited to zero. The initial elements 1, 2, 3 are pushed out. The remaining elements 4, 5, 6 move to the start. The last three positions are then padded with 50, yielding the result [4, 5, 6, 50, 50, 50].

This ability to specify arbitrary fill values makes the custom linear shifting approach highly valuable when padding needs to represent a specific condition, such as maximum capacity or a default placeholder that is distinct from the actual data range. This ensures downstream processes can easily identify the padded sections.

import numpy as np

#create NumPy array
data = np.array([1, 2, 3, 4, 5, 6])

#define custom function to shift elements
def shift_elements(arr, num, fill_value):
    result = np.empty_like(arr)
    if num > 0:
        result[:num] = fill_value
        result[num:] = arr[:-num]
    elif num < 0:
        result[num:] = fill_value
        result[:num] = arr[-num:]
    else:
        result[:] = arr
    return result

#shift each element three positions to the left and replace shifted values with 50
data_new = shift_elements(data, -3, 50)

#view new NumPy array
data_new

array([ 4,  5,  6, 50, 50, 50])

Summary and Further NumPy Operations

Shifting elements in a NumPy array is a common operation, achievable through two distinct methods tailored to different analytical needs. For scenarios requiring cyclical data rotation where all original values must be preserved, the native np.roll() function provides an optimized and straightforward solution. This method is ideal for processing circular buffers or periodic data.

For tasks demanding linear alignment or padding—where elements shifted off the boundary must be replaced by a placeholder value—a custom function utilizing slicing and masking provides the necessary control. While requiring more lines of code, this approach ensures the resulting array maintains its size and shape while correctly introducing boundary conditions defined by the fill_value.

Mastering these two shifting techniques ensures flexibility and efficiency when manipulating numerical data structures in Python. Understanding whether to use cyclic wrapping or linear padding is crucial for maintaining data integrity in downstream analyses.

Additional Resources for NumPy Mastery

The following tutorials explain how to perform other common and essential operations in NumPy, complementing your ability to manipulate array data effectively:

Cite this article

stats writer (2025). How to Easily Shift Elements in a NumPy Array. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-to-shift-elements-in-numpy-array-with-examples/

stats writer. "How to Easily Shift Elements in a NumPy Array." PSYCHOLOGICAL SCALES, 1 Dec. 2025, https://scales.arabpsychology.com/stats/how-to-shift-elements-in-numpy-array-with-examples/.

stats writer. "How to Easily Shift Elements in a NumPy Array." PSYCHOLOGICAL SCALES, 2025. https://scales.arabpsychology.com/stats/how-to-shift-elements-in-numpy-array-with-examples/.

stats writer (2025) 'How to Easily Shift Elements in a NumPy Array', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-to-shift-elements-in-numpy-array-with-examples/.

[1] stats writer, "How to Easily Shift Elements in a NumPy Array," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, December, 2025.

stats writer. How to Easily Shift Elements in a NumPy Array. PSYCHOLOGICAL SCALES. 2025;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top