Skip to content

Commit 300ae4f

Browse files
committed
fix #631 : implemented LArray.reverse() method
1 parent 2bd4569 commit 300ae4f

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

doc/source/api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ Changing Axes or Labels
329329
LArray.set_labels
330330
LArray.combine_axes
331331
LArray.split_axes
332+
LArray.reverse
332333

333334
.. _la_agg:
334335

doc/source/changes/version_0_30.rst.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ Miscellaneous improvements
221221
* renamed `a_min` and `a_max` arguments of :py:obj:`LArray.clip()` as `minval` and `maxval` respectively
222222
and made them optional (closes :issue:`747`).
223223

224+
* implemented :py:obj:`LArray.reverse()` method to reverse one or several axes of an array (closes :issue:`631`).
225+
224226

225227
Fixes
226228
-----

larray/core/array.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7144,6 +7144,66 @@ def split_axes(self, axes=None, sep='_', names=None, regex=None, sort=False, fil
71447144
return array
71457145
split_axis = renamed_to(split_axes, 'split_axis')
71467146

7147+
def reverse(self, axes=None):
7148+
r"""
7149+
Reverse axes of an array
7150+
7151+
Parameters
7152+
----------
7153+
axes : int, str, Axis or any combination of those
7154+
axes to reverse. If None, all axes are reversed. Defaults to None.
7155+
7156+
Returns
7157+
-------
7158+
LArray
7159+
Array with passed `axes` reversed.
7160+
7161+
Examples
7162+
--------
7163+
>>> arr = ndtest((2, 2, 2))
7164+
>>> arr
7165+
a b\c c0 c1
7166+
a0 b0 0 1
7167+
a0 b1 2 3
7168+
a1 b0 4 5
7169+
a1 b1 6 7
7170+
7171+
Reverse one axis
7172+
7173+
>>> arr.reverse('c')
7174+
a b\c c1 c0
7175+
a0 b0 1 0
7176+
a0 b1 3 2
7177+
a1 b0 5 4
7178+
a1 b1 7 6
7179+
7180+
Reverse several axes
7181+
7182+
>>> arr.reverse(('a', 'c'))
7183+
a b\c c1 c0
7184+
a1 b0 5 4
7185+
a1 b1 7 6
7186+
a0 b0 1 0
7187+
a0 b1 3 2
7188+
7189+
Reverse all axes
7190+
7191+
>>> arr.reverse()
7192+
a b\c c1 c0
7193+
a1 b1 7 6
7194+
a1 b0 5 4
7195+
a0 b1 3 2
7196+
a0 b0 1 0
7197+
"""
7198+
if axes is None:
7199+
axes = self.axes
7200+
else:
7201+
axes = self.axes[axes]
7202+
if not isinstance(axes, AxisCollection):
7203+
axes = AxisCollection(axes)
7204+
reversed_axes = tuple(axis[::-1] for axis in axes)
7205+
return self[reversed_axes]
7206+
71477207

71487208
def larray_equal(a1, a2):
71497209
import warnings

0 commit comments

Comments
 (0)