[Numpy-discussion] A proposed change to rollaxis() behavior for negative 'start' values

Ken Basye kbasye1 at jhu.edu
Tue Sep 21 15:48:30 EDT 2010


Hi Numpy Folks,
  A while back, I filed this ticket:  
http://projects.scipy.org/numpy/ticket/1441  suggesting a change to 
rollaxis() and some fixes to the doc and error reporting.  Ralf Gommers 
suggested I float the behavior change here, so that's what I'm doing. 

The motivation for the change comes because it seems like there should 
be a simpler way to get some axis into the last position than to do this:

 >>> a = np.ones((3,4,5,6))
 >>> b = np.rollaxis(a, axis=0, start=len(a.shape))
 >>> b.shape
(4, 5, 6, 3)

But currently it seems there isn't because when you specify -1 as the 
'start' argument, the axis is moved into the second-to-last position.  
My proposed change, which you can see on the ticket, would change that 
so that using -1 referred to the end position.  Note that the use of 
negative 'start' arguments isn't currently documented and, in its 
current form, doesn't seem very useful.  My proposal wouldn't change the 
behavior for positive 'start' values at all, and the interpretation of 
'axis' arguments is also unaffected. 

If that's going to break too much code, here's a pathway that might be 
acceptable:  Add a new function moveaxis() which works the way 
rollaxis() does for positive arguments but in the new way for negative 
arguments.  Eventually, rollaxis could be deprecated to keep things 
tidy.  This has the added advantage of using a name that seems to fit 
what the function does better - 'rollaxis' suggests a behavior like the 
roll() function which affects other axes, which isn't what happens.

Thanks for listening; I'm a big fan of Numpy.

Best,
   Ken Basye





More information about the NumPy-Discussion mailing list