Skip to content

np.swapaxes doesn't always return a view #5260

Closed

Description

@jaimefrio

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.

Activity

  1. njsmith commented on Nov 4, 2014

    @njsmith
    Member

    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:

    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.

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

  2. seberg commented on Nov 4, 2014

    @seberg
    Member

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

  3. jaimefrio commented on Nov 4, 2014

    @jaimefrio
    MemberAuthor

    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.

  4. charris commented on Nov 4, 2014

    @charris
    Member

    Always returning a view sounds right to me on first thought.

  5. charris commented on Nov 11, 2014

    @charris
    Member

    Is there agreement on always returning a view?

  6. jaimefrio commented on Nov 11, 2014

    @jaimefrio
    MemberAuthor

    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.

  7. njsmith commented on Nov 11, 2014

    @njsmith
    Member

    Everyone seems lukewarmly in favor :-)

  8. added a commit that references this issue on Jan 21, 2015
    960433e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions