diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2015-01-18 10:29:42 -0700 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2015-01-19 09:14:48 -0700 |
commit | 38b1a7c3038223ee72138413621ef3e00c9224f5 (patch) | |
tree | 4e75d189041a621ef18c09f1c35881958660c9d9 /numpy | |
parent | 5c00d82b9fdb0cb0e137ba36549ce12ad3d0d047 (diff) | |
download | numpy-38b1a7c3038223ee72138413621ef3e00c9224f5.tar.gz |
ENH: Make rollaxis always return a view.
Previous to this commit, rollaxis returned a view unless the order of
the axis was unchanged, in which case the input array was returned.
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/numeric.py | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index 0f3ee8a4d..a464562c4 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -1392,6 +1392,7 @@ def roll(a, shift, axis=None): res = res.reshape(a.shape) return res + def rollaxis(a, axis, start=0): """ Roll the specified axis backwards, until it lies in a given position. @@ -1410,7 +1411,9 @@ def rollaxis(a, axis, start=0): Returns ------- res : ndarray - A view of `a` with its axes permuted. + For Numpy >= 1.10 a view of `a` is always returned. For earlier + Numpy versions a view of `a` is returned only if the order of the + axes is changed, otherwise the input array is returned. See Also -------- @@ -1436,17 +1439,19 @@ def rollaxis(a, axis, start=0): msg = 'rollaxis: %s (%d) must be >=0 and < %d' if not (0 <= axis < n): raise ValueError(msg % ('axis', axis, n)) - if not (0 <= start < n+1): - raise ValueError(msg % ('start', start, n+1)) - if (axis < start): # it's been removed + if not (0 <= start < n + 1): + raise ValueError(msg % ('start', start, n + 1)) + if (axis < start): + # it's been removed start -= 1 - if axis==start: - return a + if axis == start: + return a[...] axes = list(range(0, n)) axes.remove(axis) axes.insert(start, axis) return a.transpose(axes) + # fix hack in scipy which imports this function def _move_axis_to_0(a, axis): return rollaxis(a, axis, 0) |