Skip to content

Commit 4460191

Browse files
committed
Modify nargsort
1 parent 8f9637c commit 4460191

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

pandas/core/sorting.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
from pandas.core.dtypes.cast import infer_dtype_from_array
1010
from pandas.core.dtypes.common import (
1111
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)
1313
from pandas.core.dtypes.missing import isna
14+
from pandas.core.dtypes.generic import ABCIndexClass
15+
1416

1517
import pandas.core.algorithms as algorithms
1618

@@ -239,12 +241,12 @@ def nargsort(items, kind='quicksort', ascending=True, na_position='last'):
239241
GH #6399, #5231
240242
"""
241243

244+
mask = isna(items)
242245
# specially handle Categorical
243246
if is_categorical_dtype(items):
244247
if na_position not in {'first', 'last'}:
245248
raise ValueError('invalid na_position: {!r}'.format(na_position))
246249

247-
mask = isna(items)
248250
cnt_null = mask.sum()
249251
sorted_idx = items.argsort(ascending=ascending, kind=kind)
250252
if ascending and na_position == 'last':
@@ -255,15 +257,19 @@ def nargsort(items, kind='quicksort', ascending=True, na_position='last'):
255257
sorted_idx = np.roll(sorted_idx, cnt_null)
256258
return sorted_idx
257259

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:
264270
items = np.asanyarray(items)
271+
265272
idx = np.arange(len(items))
266-
mask = isna(items)
267273
non_nans = items[~mask]
268274
non_nan_idx = idx[~mask]
269275
nan_idx = np.nonzero(mask)[0]

0 commit comments

Comments
 (0)