Skip to content

Commit c2578c3

Browse files
committed
Workaround Py2
1 parent e474c20 commit c2578c3

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

pandas/core/arrays/categorical.py

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,17 +1393,21 @@ def check_for_ordered(self, op):
13931393
def _values_for_argsort(self):
13941394
return self._codes.copy()
13951395

1396-
def argsort(self, ascending=True, kind='quicksort', *args, **kwargs):
1397-
"""
1398-
Returns the indices that would sort the Categorical instance if
1399-
'sort_values' was called. This function is implemented to provide
1400-
compatibility with numpy ndarray objects.
1396+
def argsort(self, *args, **kwargs):
1397+
# TODO(PY2): use correct signature
1398+
# We have to do *args, **kwargs to avoid a a py2-only signature
1399+
# issue since np.argsort differs from argsort.
1400+
"""Return the indicies that would sort the Categorical.
14011401
1402-
While an ordering is applied to the category values, arg-sorting
1403-
in this context refers more to organizing and grouping together
1404-
based on matching category values. Thus, this function can be
1405-
called on an unordered Categorical instance unlike the functions
1406-
'Categorical.min' and 'Categorical.max'.
1402+
Parameters
1403+
----------
1404+
ascending : bool, default True
1405+
Whether the indices should result in an ascending
1406+
or descending sort.
1407+
kind : {'quicksort', 'mergesort', 'heapsort'}, optional
1408+
Sorting algorithm.
1409+
args, kwargs:
1410+
passed through to :func:`numpy.argsort`.
14071411
14081412
Returns
14091413
-------
@@ -1412,10 +1416,28 @@ def argsort(self, ascending=True, kind='quicksort', *args, **kwargs):
14121416
See also
14131417
--------
14141418
numpy.ndarray.argsort
1419+
1420+
Notes
1421+
-----
1422+
While an ordering is applied to the category values, arg-sorting
1423+
in this context refers more to organizing and grouping together
1424+
based on matching category values. Thus, this function can be
1425+
called on an unordered Categorical instance unlike the functions
1426+
'Categorical.min' and 'Categorical.max'.
1427+
1428+
Examples
1429+
--------
1430+
>>> pd.Categorical(['b', 'b', 'a', 'c']).argsort()
1431+
array([2, 0, 1, 3])
1432+
1433+
>>> cat = pd.Categorical(['b', 'b', 'a', 'c'],
1434+
... categories=['c', 'b', 'a'],
1435+
... ordered=True)
1436+
>>> cat.argsort()
1437+
array([3, 0, 1, 2])
14151438
"""
14161439
# Keep the implementation here just for the docstring.
1417-
return super(Categorical, self).argsort(ascending=ascending, kind=kind,
1418-
*args, **kwargs)
1440+
return super(Categorical, self).argsort(*args, **kwargs)
14191441

14201442
def sort_values(self, inplace=False, ascending=True, na_position='last'):
14211443
""" Sorts the Categorical by category value returning a new

0 commit comments

Comments
 (0)