Skip to content

Commit

Permalink
PERF: categorical rank GH#15498
Browse files Browse the repository at this point in the history
  • Loading branch information
jeet63 committed Mar 1, 2017
1 parent d0a281f commit 33249b3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
8 changes: 5 additions & 3 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -974,9 +974,6 @@ def _get_data_algo(values, func_map):

f = None

if is_categorical_dtype(values):
values = values._values_for_rank()

if is_float_dtype(values):
f = func_map['float64']
values = _ensure_float64(values)
Expand All @@ -992,6 +989,11 @@ def _get_data_algo(values, func_map):
elif is_unsigned_integer_dtype(values):
f = func_map['uint64']
values = _ensure_uint64(values)

elif is_categorical_dtype(values):
f = func_map['float64']
values = values._values_for_rank()

else:
values = _ensure_object(values)

Expand Down
7 changes: 5 additions & 2 deletions pandas/core/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1416,14 +1416,17 @@ def _values_for_rank(self):
numpy array
"""
from pandas import Series
if self.ordered:
values = self.codes
mask = values == -1
values = values.astype('float64')
if mask.any():
values = values.astype('float64')
values[mask] = np.nan
else:
values = np.array(self)
values = np.array(
self.rename_categories(Series(self.categories).rank())
)
return values

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

0 comments on commit 33249b3

Please sign in to comment.