93
93
class MaskedArrayFutureWarning (FutureWarning ):
94
94
pass
95
95
96
+ def _deprecate_argsort_axis (arr ):
97
+ """
98
+ Adjust the axis passed to argsort, warning if necessary
99
+
100
+ Parameters
101
+ ----------
102
+ arr
103
+ The array which argsort was called on
104
+
105
+ np.ma.argsort has a long-term bug where the default of the axis argument
106
+ is wrong (gh-8701), which now must be kept for backwards compatibiity.
107
+ Thankfully, this only makes a difference when arrays are 2- or more-
108
+ dimensional, so we only need a warning then.
109
+ """
110
+ if arr .ndim <= 1 :
111
+ # no warning needed - but switch to -1 anyway, to avoid surprising
112
+ # subclasses, which are more likely to implement scalar axes.
113
+ return - 1
114
+ else :
115
+ # 2017-04-11, Numpy 1.13.0, gh-8701: warn on axis default
116
+ warnings .warn (
117
+ "In the future the default for argsort will be axis=-1, not the "
118
+ "current None, to match its documentation and np.argsort. "
119
+ "Explicitly pass -1 or None to silence this warning." ,
120
+ MaskedArrayFutureWarning , stacklevel = 3 )
121
+ return None
122
+
96
123
97
124
def doc_note (initialdoc , note ):
98
125
"""
@@ -5285,7 +5312,7 @@ def round(self, decimals=0, out=None):
5285
5312
out .__setmask__ (self ._mask )
5286
5313
return out
5287
5314
5288
- def argsort (self , axis = None , kind = 'quicksort' , order = None ,
5315
+ def argsort (self , axis = np . _NoValue , kind = 'quicksort' , order = None ,
5289
5316
endwith = True , fill_value = None ):
5290
5317
"""
5291
5318
Return an ndarray of indices that sort the array along the
@@ -5295,8 +5322,15 @@ def argsort(self, axis=None, kind='quicksort', order=None,
5295
5322
Parameters
5296
5323
----------
5297
5324
axis : int, optional
5298
- Axis along which to sort. The default is -1 (last axis).
5299
- If None, the flattened array is used.
5325
+ Axis along which to sort. If None, the default, the flattened array
5326
+ is used.
5327
+
5328
+ .. versionchanged:: 1.13.0
5329
+ Previously, the default was documented to be -1, but that was
5330
+ in error. At some future date, the default will change to -1, as
5331
+ originally intended.
5332
+ Until then, the axis should be given explicitly when
5333
+ ``arr.ndim > 1``, to avoid a FutureWarning.
5300
5334
kind : {'quicksort', 'mergesort', 'heapsort'}, optional
5301
5335
Sorting algorithm.
5302
5336
order : list, optional
@@ -5341,6 +5375,10 @@ def argsort(self, axis=None, kind='quicksort', order=None,
5341
5375
5342
5376
"""
5343
5377
5378
+ # 2017-04-11, Numpy 1.13.0, gh-8701: warn on axis default
5379
+ if axis is np ._NoValue :
5380
+ axis = _deprecate_argsort_axis (self )
5381
+
5344
5382
if fill_value is None :
5345
5383
if endwith :
5346
5384
# nan > inf
@@ -6560,10 +6598,14 @@ def power(a, b, third=None):
6560
6598
argmin = _frommethod ('argmin' )
6561
6599
argmax = _frommethod ('argmax' )
6562
6600
6563
- def argsort (a , axis = None , kind = 'quicksort' , order = None , endwith = True , fill_value = None ):
6601
+ def argsort (a , axis = np . _NoValue , kind = 'quicksort' , order = None , endwith = True , fill_value = None ):
6564
6602
"Function version of the eponymous method."
6565
6603
a = np .asanyarray (a )
6566
6604
6605
+ # 2017-04-11, Numpy 1.13.0, gh-8701: warn on axis default
6606
+ if axis is np ._NoValue :
6607
+ axis = _deprecate_argsort_axis (a )
6608
+
6567
6609
if isinstance (a , MaskedArray ):
6568
6610
return a .argsort (axis = axis , kind = kind , order = order ,
6569
6611
endwith = endwith , fill_value = fill_value )
0 commit comments