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)
.