Skip to content

Commit 22ae6ac

Browse files
committed
fix #490 : added argument reverse to methods labelsofsorted and indicesofsorted
1 parent 96b37fd commit 22ae6ac

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

doc/source/changes/version_0_27.rst.inc

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,27 @@
33

44
* added a feature (see the :ref:`miscellaneous section <misc>` for details).
55

6-
* added another feature.
6+
* added `reverse` argument to methods `indicesofsorted` and `labelsofsorted` to sort values in descending order
7+
instead of ascending (default behavior):
78

8-
.. _misc:
9+
>>> arr = LArray([[1, 5], [3, 2], [0, 4]], "nat=BE,FR,IT; sex=M,F")
10+
>>> arr
11+
nat\sex M F
12+
BE 1 5
13+
FR 3 2
14+
IT 0 4
15+
>>> arr.indicesofsorted("nat", reverse=True)
16+
nat\sex M F
17+
0 1 0
18+
1 0 2
19+
2 2 1
20+
>>> arr.labelsofsorted("nat", reverse=True)
21+
nat\sex M F
22+
0 FR BE
23+
1 BE IT
24+
2 IT FR
25+
26+
Closes :issue:`490`.
927

1028
Miscellaneous improvements
1129
--------------------------
@@ -15,4 +33,4 @@ Miscellaneous improvements
1533
Fixes
1634
-----
1735

18-
* fixed something (closes :issue:`1`).
36+
* fixed something (closes :issue:`1`).

larray/core/array.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3076,7 +3076,7 @@ def indexofmax(self, axis=None):
30763076

30773077
posargmax = renamed_to(indexofmax, 'posargmax')
30783078

3079-
def labelsofsorted(self, axis=None, kind='quicksort'):
3079+
def labelsofsorted(self, axis=None, reverse=False, kind='quicksort'):
30803080
"""Returns the labels that would sort this array.
30813081
30823082
Performs an indirect sort along the given axis using the algorithm specified by the `kind` keyword. It returns
@@ -3086,6 +3086,8 @@ def labelsofsorted(self, axis=None, kind='quicksort'):
30863086
----------
30873087
axis : int or str or Axis, optional
30883088
Axis along which to sort. This can be omitted if array has only one axis.
3089+
reverse : bool, optional
3090+
Sort values in descending order. Defaults to False (ascending order).
30893091
kind : {'quicksort', 'mergesort', 'heapsort'}, optional
30903092
Sorting algorithm. Defaults to 'quicksort'.
30913093
@@ -3108,18 +3110,23 @@ def labelsofsorted(self, axis=None, kind='quicksort'):
31083110
BE M F
31093111
FR F M
31103112
IT M F
3113+
>>> arr.labelsofsorted(X.sex, reverse=True)
3114+
nat\\sex 0 1
3115+
BE F M
3116+
FR M F
3117+
IT F M
31113118
"""
31123119
if axis is None:
31133120
if self.ndim > 1:
3114-
raise ValueError("array has ndim > 1 and no axis specified for argsort")
3121+
raise ValueError("array has ndim > 1 and no axis specified for labelsofsorted")
31153122
axis = self.axes[0]
31163123
axis = self.axes[axis]
3117-
pos = self.indicesofsorted(axis, kind=kind)
3124+
pos = self.indicesofsorted(axis, reverse=reverse, kind=kind)
31183125
return LArray(axis.labels[pos.data], pos.axes)
31193126

31203127
argsort = renamed_to(labelsofsorted, 'argsort')
31213128

3122-
def indicesofsorted(self, axis=None, kind='quicksort'):
3129+
def indicesofsorted(self, axis=None, reverse=False, kind='quicksort'):
31233130
"""Returns the indices that would sort this array.
31243131
31253132
Performs an indirect sort along the given axis using the algorithm specified by the `kind` keyword. It returns
@@ -3129,6 +3136,8 @@ def indicesofsorted(self, axis=None, kind='quicksort'):
31293136
----------
31303137
axis : int or str or Axis, optional
31313138
Axis along which to sort. This can be omitted if array has only one axis.
3139+
reverse : bool, optional
3140+
Sort values in descending order. Defaults to False (ascending order).
31323141
kind : {'quicksort', 'mergesort', 'heapsort'}, optional
31333142
Sorting algorithm. Defaults to 'quicksort'.
31343143
@@ -3151,13 +3160,22 @@ def indicesofsorted(self, axis=None, kind='quicksort'):
31513160
0 2 1
31523161
1 0 2
31533162
2 1 0
3163+
>>> arr.indicesofsorted(X.nat, reverse=True)
3164+
nat\\sex M F
3165+
0 1 0
3166+
1 0 2
3167+
2 2 1
31543168
"""
31553169
if axis is None:
31563170
if self.ndim > 1:
3157-
raise ValueError("array has ndim > 1 and no axis specified for posargsort")
3171+
raise ValueError("array has ndim > 1 and no axis specified for indicesofsorted")
31583172
axis = self.axes[0]
31593173
axis, axis_idx = self.axes[axis], self.axes.index(axis)
31603174
data = self.data.argsort(axis_idx, kind=kind)
3175+
if reverse:
3176+
reverser = tuple(slice(None, None, -1) if i == axis_idx else slice(None)
3177+
for i in range(self.ndim))
3178+
data = data[reverser]
31613179
new_axis = Axis(np.arange(len(axis)), axis.name)
31623180
return LArray(data, self.axes.replace(axis, new_axis))
31633181

0 commit comments

Comments
 (0)