You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Related somehow to the discussion in #5258. The docs for np.swapaxes state that it returns a view if the first argument is an ndarray, but it has a fast path for axis1 == axis2, where it actually returns the original array incref'ed:
>>> a = np.arange(60).reshape(3, 4, 5).copy()
>>> a.base is None
True
>>> a.swapaxes(0, 1).base is None
False
>>> a.swapaxes(0, 0).base is None
True
>>> a.swapaxes(20, 20).base is None # that this works is bad!!!
True
>>> a.transpose(range(3)).base is None
False
I prefer that it always returned a view, even if it is of the whole array unchanged, as the docs say, and as e.g. np.transpose does, even if the array is unchanged, and will be more than happy to submit a PR. If there is any good reason why this should not be the case, then at least the docs should be updated, and the check for axis1 == axis2 be moved to after they have both been checked for in-boundness.
I agree that the lack of bounds checking is a bug. I'm not sure it matters
whether we return a view or the original array -- I guess it affects things
like .resize? -- but if you want to fix it then I can't see any problem
with that either.
Related somehow to the discussion in #5258 #5258. The docs for np.swapaxes
state that it returns a view if the first argument is an ndarray, but it
has a fast path for axis1 == axis2, where it actually returns the
original array incref'ed:
a = np.arange(60).reshape(3, 4, 5).copy()
a.base is None
True
a.swapaxes(0, 1).base is None
False
a.swapaxes(0, 0).base is None
True
a.swapaxes(20, 20).base is None # that this works is bad!!!
True
a.transpose(range(3)).base is None
False
I prefer that it always returned a view, even if it is of the whole array
unchanged, as the docs say, and as e.g. np.transpose does, even if the
array is unchanged, and will be more than happy to submit a PR. If there is
any good reason why this should not be the case, then at least the docs
should be updated, and the check for axis1 == axis2 be moved to after
they have both been checked for in-boundness.
—
Reply to this email directly or view it on GitHub #5260.
Sounds reasonable. I guess ideally returning the array or returning a view does not really matter, because ndarray is almost immutable (the container part). Resize and arr.shape = (1, 2, 3)/arr.strides = (1, 2) and even arr.dtype are notable exception. Except shape, none of these are probably used, but then view creation should be fast enough...
There is actually the exact same issue with rollaxis, see here. I had some old code that tripped on this, but I can't fully recall what the exact issue was.
Activity
njsmith commentedon Nov 4, 2014
I agree that the lack of bounds checking is a bug. I'm not sure it matters
whether we return a view or the original array -- I guess it affects things
like .resize? -- but if you want to fix it then I can't see any problem
with that either.
On Tue, Nov 4, 2014 at 5:44 PM, Jaime [email protected] wrote:
Nathaniel J. Smith
Postdoctoral researcher - Informatics - University of Edinburgh
http://vorpus.org
seberg commentedon Nov 4, 2014
Sounds reasonable. I guess ideally returning the array or returning a view does not really matter, because
ndarrayis almost immutable (the container part). Resize andarr.shape = (1, 2, 3)/arr.strides = (1, 2)and evenarr.dtypeare notable exception. Except shape, none of these are probably used, but then view creation should be fast enough...jaimefrio commentedon Nov 4, 2014
There is actually the exact same issue with
rollaxis, see here. I had some old code that tripped on this, but I can't fully recall what the exact issue was.charris commentedon Nov 4, 2014
Always returning a view sounds right to me on first thought.
charris commentedon Nov 11, 2014
Is there agreement on always returning a view?
jaimefrio commentedon Nov 11, 2014
My work schedule has been crazy this past few weeks, but I will make a pull request later tonight. Will also ping the list when it is ready.
njsmith commentedon Nov 11, 2014
Everyone seems lukewarmly in favor :-)
Merge pull request #5468 from jaimefrio/swapaxes_view