9
9
from pandas .core .dtypes .cast import infer_dtype_from_array
10
10
from pandas .core .dtypes .common import (
11
11
ensure_int64 , ensure_platform_int , is_categorical_dtype ,
12
- is_extension_array_dtype , is_list_like )
12
+ is_extension_array_dtype , is_list_like , is_sparse )
13
13
from pandas .core .dtypes .missing import isna
14
+ from pandas .core .dtypes .generic import ABCIndexClass
15
+
14
16
15
17
import pandas .core .algorithms as algorithms
16
18
@@ -239,12 +241,12 @@ def nargsort(items, kind='quicksort', ascending=True, na_position='last'):
239
241
GH #6399, #5231
240
242
"""
241
243
244
+ mask = isna (items )
242
245
# specially handle Categorical
243
246
if is_categorical_dtype (items ):
244
247
if na_position not in {'first' , 'last' }:
245
248
raise ValueError ('invalid na_position: {!r}' .format (na_position ))
246
249
247
- mask = isna (items )
248
250
cnt_null = mask .sum ()
249
251
sorted_idx = items .argsort (ascending = ascending , kind = kind )
250
252
if ascending and na_position == 'last' :
@@ -255,15 +257,19 @@ def nargsort(items, kind='quicksort', ascending=True, na_position='last'):
255
257
sorted_idx = np .roll (sorted_idx , cnt_null )
256
258
return sorted_idx
257
259
258
- with warnings .catch_warnings ():
259
- # https://github.com/pandas-dev/pandas/issues/25439
260
- # can be removed once ExtensionArrays are properly handled by nargsort
261
- warnings .filterwarnings (
262
- "ignore" , category = FutureWarning ,
263
- message = "Converting timezone-aware DatetimeArray to" )
260
+ if (not isinstance (items , ABCIndexClass )
261
+ and is_extension_array_dtype (items )):
262
+
263
+ if is_sparse (items ):
264
+ # The conversion to np.ndarray is the fact that
265
+ # SparseArray.isna() is also a SparseArray
266
+ mask = np .array (isna (items ))
267
+
268
+ items = items ._values_for_argsort ()
269
+ else :
264
270
items = np .asanyarray (items )
271
+
265
272
idx = np .arange (len (items ))
266
- mask = isna (items )
267
273
non_nans = items [~ mask ]
268
274
non_nan_idx = idx [~ mask ]
269
275
nan_idx = np .nonzero (mask )[0 ]
0 commit comments