Say I have a numpy array:
>>> a
array([0,1,2,3,4])
and I want to "rotate" it to get:
>>> b
array([4,0,1,2,3])
What is the best way?
I have been converting to a deque and back (see below) but is there a better way?
b = deque(a)
b.rotate(1)
b = np.array(b)
a.shapeis(n,)not(n,1)- askewchanrollfunction, but be careful in some cases wherendimmight matter (fora.shapeis(n,),a.ndimis1; but for shape(n,1),a.ndimis 2). As you can see from the question you linked to, an axis must be added to get from the 1d to 2d case. - askewchanawith an (n,1) shape asarray([[0],[1],[2],[3],[4]]). However, each of the answers does work for both (n,) and (n,1) shapes. - atomh33lsrollwill be effectively be applied along the non-1 axis if there is only one, which is why my comment was nothing beyond pedantry :). But if your array is(n,m)(or higher) it will roll along all the axes (the flattened array) which might give unexpected results. The solution there is to just donp.roll(a, axis=0)- askewchan