What is the numpy.roll() in Python?
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
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
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.
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
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
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.




