Skip to content

Commit 73a1e4d

Browse files
authored
TST, DOC: add doc and test for transpose axes with negative indices (numpy#27101)
Description This PR updates the documentation to clarify that negative indices are supported in the axes parameter of np.transpose and add test for it. Changes made Updated the docstring and for np.transpose to explicitly mention support for negative indices Add test for np.transpose Close numpy#27024
1 parent 5cec054 commit 73a1e4d

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

numpy/_core/fromnumeric.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -653,10 +653,11 @@ def transpose(a, axes=None):
653653
Input array.
654654
axes : tuple or list of ints, optional
655655
If specified, it must be a tuple or list which contains a permutation
656-
of [0,1,...,N-1] where N is the number of axes of `a`. The `i`'th axis
657-
of the returned array will correspond to the axis numbered ``axes[i]``
658-
of the input. If not specified, defaults to ``range(a.ndim)[::-1]``,
659-
which reverses the order of the axes.
656+
of [0, 1, ..., N-1] where N is the number of axes of `a`. Negative
657+
indices can also be used to specify axes. The i-th axis of the returned
658+
array will correspond to the axis numbered ``axes[i]`` of the input.
659+
If not specified, defaults to ``range(a.ndim)[::-1]``, which reverses
660+
the order of the axes.
660661
661662
Returns
662663
-------
@@ -699,6 +700,10 @@ def transpose(a, axes=None):
699700
>>> np.transpose(a).shape
700701
(5, 4, 3, 2)
701702
703+
>>> a = np.arange(3*4*5).reshape((3, 4, 5))
704+
>>> np.transpose(a, (-1, 0, -2)).shape
705+
(5, 3, 4)
706+
702707
"""
703708
return _wrapfunc(a, 'transpose', axes)
704709

numpy/_core/tests/test_numeric.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ def test_transpose(self):
343343
arr = [[1, 2], [3, 4], [5, 6]]
344344
tgt = [[1, 3, 5], [2, 4, 6]]
345345
assert_equal(np.transpose(arr, (1, 0)), tgt)
346+
assert_equal(np.transpose(arr, (-1, -2)), tgt)
346347
assert_equal(np.matrix_transpose(arr), tgt)
347348

348349
def test_var(self):

0 commit comments

Comments
 (0)