Skip to content

Commit 20a81a5

Browse files
committed
ask Categorical for ranking function
1 parent b0d19d7 commit 20a81a5

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

pandas/core/algorithms.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -988,8 +988,8 @@ def _get_data_algo(values, func_map):
988988
elif is_unsigned_integer_dtype(values):
989989
f = func_map['uint64']
990990
values = _ensure_uint64(values)
991-
elif is_categorical(values) and values._ordered:
992-
f = func_map['float64']
991+
elif is_categorical_dtype(values):
992+
f = values._funcs_for_rank()
993993
values = values._values_for_rank()
994994
else:
995995
values = _ensure_object(values)

pandas/core/categorical.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1404,6 +1404,25 @@ def sort_values(self, inplace=False, ascending=True, na_position='last'):
14041404
return self._constructor(values=codes, categories=self.categories,
14051405
ordered=self.ordered, fastpath=True)
14061406

1407+
def _funcs_for_rank(self):
1408+
"""
1409+
For correctly ranking ordered categorical data. See GH#15420
1410+
1411+
Ordered categorical data should be ranked on the basis of
1412+
codes with -1 translated to NaN, as floats, and unordered
1413+
as objects
1414+
1415+
Returns
1416+
-------
1417+
numpy array
1418+
1419+
"""
1420+
if self._ordered:
1421+
f = _algos.rank_1d_float64
1422+
else:
1423+
f = _algos.rank_1d_object
1424+
return f
1425+
14071426
def _values_for_rank(self):
14081427
"""
14091428
For correctly ranking ordered categorical data. See GH#15420
@@ -1416,10 +1435,12 @@ def _values_for_rank(self):
14161435
numpy array
14171436
14181437
"""
1419-
values = self._codes.astype('float64')
14201438
if self._ordered:
1439+
values = self._codes.astype('float64')
14211440
na_mask = (values == -1)
14221441
values[na_mask] = np.nan
1442+
else:
1443+
values = np.array(self)
14231444
return values
14241445

14251446
def order(self, inplace=False, ascending=True, na_position='last'):

0 commit comments

Comments
 (0)