Skip to content

Rank categorical perf #15518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions asv_bench/benchmarks/categoricals.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,49 @@ def time_value_counts_dropna(self):

def time_rendering(self):
str(self.sel)


class Categoricals3(object):
goal_time = 0.2

def setup(self):
n = 100

strng = pd.util.testing.makeCategoricalIndex(n)
self.s1 = pd.Series(strng)

dt = pd.util.testing.makeDateIndex(n)
self.s2 = pd.Series(dt).astype('category', categories=dt)
self.s2o = pd.Series(dt).astype('category', categories=dt,
ordered=True)

fl = pd.util.testing.makeFloatIndex(n)
self.s3 = pd.Series(fl).astype('category', categories=fl)
self.s3o = pd.Series(fl).astype('category', categories=fl,
ordered=True)

intg = pd.util.testing.makeIntIndex(n)
self.s4 = pd.Series(intg).astype('category', categories=intg)
self.s4o = pd.Series(intg).astype('category', categories=intg,
ordered=True)

def time_rank_string_unordered(self):
self.s1.rank()

def time_rank_dt_unordered(self):
self.s2.rank()

def time_rank_dt_ordered(self):
self.s2o.rank()

def time_rank_float_unordered(self):
self.s3.rank()

def time_rank_float_ordered(self):
self.s3o.rank()

def time_rank_int_unordered(self):
self.s4.rank()

def time_rank_int_ordered(self):
self.s4o.rank()
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ Performance Improvements
- Improved performance of ``groupby().cummin()`` and ``groupby().cummax()`` (:issue:`15048`, :issue:`15109`)
- Improved performance and reduced memory when indexing with a ``MultiIndex`` (:issue:`15245`)
- When reading buffer object in ``read_sas()`` method without specified format, filepath string is inferred rather than buffer object. (:issue:`14947`)
- Improved performance of `rank()` for categorical data (:issue:`15498`)



Expand Down
1 change: 1 addition & 0 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,7 @@ def _get_data_algo(values, func_map):
elif is_unsigned_integer_dtype(values):
f = func_map['uint64']
values = _ensure_uint64(values)

else:
values = _ensure_object(values)

Expand Down
9 changes: 8 additions & 1 deletion pandas/core/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1416,14 +1416,21 @@ def _values_for_rank(self):
numpy array

"""
from pandas import Series
if self.ordered:
values = self.codes
mask = values == -1
if mask.any():
values = values.astype('float64')
values[mask] = np.nan
else:
elif self.categories.is_numeric():
values = np.array(self)
else:
# reorder the categories (so rank can use the float codes)
# instead of passing an object array to rank
values = np.array(
self.rename_categories(Series(self.categories).rank())
)
return values

def order(self, inplace=False, ascending=True, na_position='last'):
Expand Down
33 changes: 25 additions & 8 deletions pandas/tests/series/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1065,8 +1065,10 @@ def test_rank_categorical(self):
exp_desc = pd.Series([6., 5., 4., 3., 2., 1.])
ordered = pd.Series(
['first', 'second', 'third', 'fourth', 'fifth', 'sixth']
).astype('category', ).cat.set_categories(
['first', 'second', 'third', 'fourth', 'fifth', 'sixth'],
).astype(
'category',
categories=['first', 'second', 'third',
'fourth', 'fifth', 'sixth'],
ordered=True
)
assert_series_equal(ordered.rank(), exp)
Expand All @@ -1075,19 +1077,33 @@ def test_rank_categorical(self):
# Unordered categoricals should be ranked as objects
unordered = pd.Series(
['first', 'second', 'third', 'fourth', 'fifth', 'sixth'],
).astype('category').cat.set_categories(
['first', 'second', 'third', 'fourth', 'fifth', 'sixth'],
).astype(
'category',
categories=['first', 'second', 'third',
'fourth', 'fifth', 'sixth'],
ordered=False
)
exp_unordered = pd.Series([2., 4., 6., 3., 1., 5.])
res = unordered.rank()
assert_series_equal(res, exp_unordered)

unordered1 = pd.Series(
[1, 2, 3, 4, 5, 6],
).astype(
'category',
categories=[1, 2, 3, 4, 5, 6],
ordered=False
)
exp_unordered1 = pd.Series([1., 2., 3., 4., 5., 6.])
res1 = unordered1.rank()
assert_series_equal(res1, exp_unordered1)

# Test na_option for rank data
na_ser = pd.Series(
['first', 'second', 'third', 'fourth', 'fifth', 'sixth', np.NaN]
).astype('category', ).cat.set_categories(
[
).astype(
'category',
categories=[
'first', 'second', 'third', 'fourth',
'fifth', 'sixth', 'seventh'
],
Expand Down Expand Up @@ -1123,8 +1139,9 @@ def test_rank_categorical(self):
# Test with pct=True
na_ser = pd.Series(
['first', 'second', 'third', 'fourth', np.NaN],
).astype('category').cat.set_categories(
['first', 'second', 'third', 'fourth'],
).astype(
'category',
categories=['first', 'second', 'third', 'fourth'],
ordered=True
)
exp_top = pd.Series([0.4, 0.6, 0.8, 1., 0.2])
Expand Down