Skip to content

DOC rollaxis return value is a view of input - #5258

Closed
jnothman wants to merge 2 commits into
numpy:masterfrom
jnothman:patch-5
Closed

jnothman wants to merge 2 commits into
numpy:masterfrom
jnothman:patch-5

Conversation

@jnothman

@jnothman jnothman commented Nov 4, 2014

Copy link
Copy Markdown
Member

No description provided.

Comment thread numpy/core/numeric.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is always possible to return a view.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. I copied from transpose and thought it safer to keep the hedge until it could be confirmed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, it sounds like transpose documentation should be fixed as well.

@charris

charris commented Nov 11, 2014

Copy link
Copy Markdown
Member

Is this correct for the current verson? I'm thinking of the comments at #5260.

@jaimefrio

Copy link
Copy Markdown
Member

No it is not:

>>> np.__version__
'1.10.0.dev-0625671'
>>> a = np.arange(60).reshape(3, 4, 5).copy()
>>> a.base is None
True
>>> np.rollaxis(a, 1).base is None
False
>>> np.rollaxis(a, 0).base is None
True

This is easy to fix: in line 1425, replace the current

if axis==start:
    return a

with

if axis==start:
    return a[...]

@charris

charris commented Nov 11, 2014

Copy link
Copy Markdown
Member

If we make this change, it needs to be mentioned in the 1.10 release notes, maybe in the compatibility section.

@jnothman

Copy link
Copy Markdown
Member Author

So in no case is the array copied, but currently there are some cases where the input is returned without creating a new view. I don't see why making it always return a view would change any intentional behaviour, but it might be considered a bug-fix.

@charris

charris commented Nov 11, 2014

Copy link
Copy Markdown
Member

You would be surprised how much code can break because it depends on such "features", often by accident ;)

@jnothman

Copy link
Copy Markdown
Member Author

Yes, I already know to be surprised... But, really, users taking advantage of a no-op call to mutate the array attributes?

@seberg

seberg commented Nov 11, 2014

Copy link
Copy Markdown
Member

Frankly, didn't hear complains about arr[...] yet, so probably not very likely, but it is surprising sometimes...

@njsmith

njsmith commented Nov 12, 2014

Copy link
Copy Markdown
Member

To be fair, it's about as likely in this case that there is code which
already assumes that it always returns a view, and will (continue to) be
broken if we do nothing :-)

On Tue, Nov 11, 2014 at 11:59 PM, seberg [email protected] wrote:

Frankly, didn't hear complains about arr[...] yet, so probably not very
likely, but it is surprising sometimes...

—
Reply to this email directly or view it on GitHub
#5258 (comment).

Nathaniel J. Smith
Postdoctoral researcher - Informatics - University of Edinburgh
http://vorpus.org

@charris

charris commented Nov 29, 2014

Copy link
Copy Markdown
Member

@jnothman Could you make the change suggested by @jaimefrio. Always returning a view seems to be the consensus.

@charris

charris commented Jan 19, 2015

Copy link
Copy Markdown
Member

Rolled into #5464, so closing this. Thanks @jnothman.

@charris charris closed this Jan 19, 2015
@jnothman

Copy link
Copy Markdown
Member Author

Ah, yes, sorry I didn't get back on this. I hadn't been sure what the exact fix should be wrt dev comments above. I should have commented so, but I'm glad it's being fixed up in any case.

@jaimefrio jaimefrio mentioned this pull request Mar 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants