python numpy roll with padding

I don't think that you are going to find an easier way to do this that is built-in. The touch-up seems quite simple to me:

y = np.roll(x,1,axis=1)
y[:,0] = 0

If you want this to be more direct then maybe you could copy the roll function to a new function and change it to do what you want. The roll() function is in the site-packages\core\numeric.py file.


There is a new numpy function in version 1.7.0 numpy.pad that can do this in one-line. Pad seems to be quite powerful and can do much more than a simple "roll". The tuple ((0,0),(1,0)) used in this answer indicates the "side" of the matrix which to pad.

import numpy as np
x = np.array([[1, 2, 3],[4, 5, 6]])

print np.pad(x,((0,0),(1,0)), mode='constant')[:, :-1]

Giving

[[0 1 2]
 [0 4 5]]