What is the numpy.roll() in Python?

Learn via video courses
Topics Covered

NumPy roll() function helps us to rotate the orientation of a NumPy array by a specified value. In simple words, it helps us to shift the elements of an array from the left to the right direction.

Let's consider an array arr [1, 2, 3, 4]. To shift all of the elements by a factor of 1, the array would be changed to [4, 1, 2, 3].

In this article, we will learn about the syntax and parameters of the NumPy roll() function, along with some examples for better understanding.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Syntax for NumPy roll()

The syntax for NumPy roll() function is:

Parameters for NumPy roll()

The parameters for NumPy roll() function are:

  • arr: This mandatory parameter represents the input array.
  • shift: The value of this necessary parameter indicates how many times the array elements need to be shifted from left to right.
  • axis: This optional parameter indicates the plane along which we want to roll/shift our input array.
Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science

Return Type for NumPy roll()

The return type for NumPy roll() is a shifted NumPy array with the same dimensions and specifications as the input array.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Examples for NumPy roll()

  • Rolling a flat array

    In this example, we will roll/shift a one-dimensional array using the NumPy roll() function in Python.

    Output

  • Rolling a two-dimensional array

    In this example, we will roll/shift a two-dimensional array using the NumPy roll() function in Python. We will also keep changing the factor of roll/shift.

    Output

  • Rolling a NumPy array by row

    In this example, we will rotate an array row-wise only, using the axis parameter. We do this by specifying the axis to be 0.

    Output

  • Rolling a NumPy array by column

    In this example, we will rotate an array column-wise only, using the axis parameter. We do this by specifying the axis to be 1.

    Output

Conclusion

  • In this article, we learned about the NumPy roll() function, a function in NumPy that allows us to shift/roll the elements of an array by a specified factor from left to right.
  • For a better understanding of the NumPy roll() function, we demonstrated different scenarios and examples where we use the NumPy roll() function to shift an array.
  • We covered shifting a whole array and shifting elements row and column-wise using the axis parameter.