Repository navigation
Description
rollaxis has an optional start parameter that, if specified, moves the axis to the position before start. Or, as the documentation puts it
The axis is rolled until it lies before this position. The default, 0, results in a “complete” roll.
(Just by the way: The last sentence is unclear. What's a "complete roll"? Better be explicit: The default, 0, moves the axis to the beginning.)
E.g.,
a = numpy.ones((1, 2, 3, 4))
b = numpy.rollaxis(a, 0, 3)
print(b.shape)moves the axis at position 0 to the place before position 3, and hence the above will print (2, 3, 1, 4).
This before logic results in the fact that there are always two start indices which will do exactly the same thing, namely numpy.rollaxis(a, i, i) and numpy.rollaxis(a, i, i+1) – nothing. As a trade-off for this duplication, what you can't seem to do is to move the axis in the last place, since there is no axis that comes after.
I used the word seem, because you can actually do numpy.rollaxis(a, i, len(a.shape)) to insert it axis i before the imaginary axis at position len(a.shape). This is not explicitly documented.
On top of that, why is the parameter called start? I have no idea.
Edit The following suggestion is actually just what moveaxis does.
A more consistent approach would be to have a parameter target (or target_position) that can be used to specify the position of axis i after the roll. E.g.,
a = numpy.ones((1, 2, 3, 4))
numpy.rollaxis(a, 0, target=0).shape # (1, 2, 3, 4)
numpy.rollaxis(a, 0, target=1).shape # (2, 1, 3, 4)
numpy.rollaxis(a, 0, target=len(a.shape)) # error
numpy.rollaxis(a, 0, target=-1).shape # (2, 3, 4, 1)The last example shows how to move the first axis to the last position without recalling len(a.shape).
Activity
eric-wieser commentedon Jul 27, 2017
Are you looking for
np.moveaxis? I've also always found rollaxis frustrating.nschloe commentedon Jul 27, 2017
Ah, yes. Now I'm asking myself why there is such a thing as
rollaxisat all.eric-wieser commentedon Jul 27, 2017
Relatedly, rollaxis is the only function taking an axis parameter that can't use normalize_axis_index
[-]suggestion: fix inconsistent start parameter in rollaxis[/-][+]rollaxis is weird[/+]eric-wieser commentedon Jul 27, 2017
It might be nice to replace all internal uses of rollaxis with moveaxis, and eventually deprecate it
nschloe commentedon Jul 27, 2017
The 1.11 changelog says about the then-new
moveaxis:grepping shows it shouldn't be too hard to get rid of
rollaxisin numpy's own code, let me try and prepare something.eric-wieser commentedon Dec 12, 2017
I think this is as fixed as it can become as of #9475, so closing. Thanks!