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