Skip to content

PERF: improve perf. of Categorical.searchsorted #28795

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

Merged
merged 3 commits into from
Oct 6, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 14 additions & 0 deletions asv_bench/benchmarks/categoricals.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,18 @@ def time_sort_values(self):
self.index.sort_values(ascending=False)


class SearchSorted:
def setup(self):
N = 10 ** 5
self.ci = tm.makeCategoricalIndex(N).sort_values()
self.c = self.ci.values
self.key = self.ci.categories[1]

def time_categorical_index_contains(self):
self.ci.searchsorted(self.key)

def time_categorical_contains(self):
self.c.searchsorted(self.key)


from .pandas_vb_common import setup # noqa: F401 isort:skip
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ Performance improvements
- Performance improvement in :meth:`DataFrame.corr` when ``method`` is ``"spearman"`` (:issue:`28139`)
- Performance improvement in :meth:`DataFrame.replace` when provided a list of values to replace (:issue:`28099`)
- Performance improvement in :meth:`DataFrame.select_dtypes` by using vectorization instead of iterating over a loop (:issue:`28317`)
- Performance improvement in :meth:`Categorical.searchsorted` and :meth:`CategoricalIndex.searchsorted` (:issue:`28795`)

.. _whatsnew_1000.bug_fixes:

Expand Down
14 changes: 6 additions & 8 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1399,14 +1399,12 @@ def memory_usage(self, deep=False):
@Substitution(klass="Categorical")
@Appender(_shared_docs["searchsorted"])
def searchsorted(self, value, side="left", sorter=None):
from pandas.core.series import Series

codes = _get_codes_for_values(Series(value).values, self.categories)
if -1 in codes:
raise KeyError("Value(s) to be inserted must be in categories.")

codes = codes[0] if is_scalar(value) else codes

if is_scalar(value):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, i would add a comment here that this is perf sensitive

codes = self.categories.get_loc(value)
codes = self.codes.dtype.type(codes)
else:
locs = [self.categories.get_loc(x) for x in value]
codes = np.array(locs, dtype=self.codes.dtype)
return self.codes.searchsorted(codes, side=side, sorter=sorter)

def isna(self):
Expand Down
8 changes: 7 additions & 1 deletion pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pandas._libs.hashtable import duplicated_int64
import pandas.compat as compat
from pandas.compat.numpy import function as nv
from pandas.util._decorators import Appender, cache_readonly
from pandas.util._decorators import Appender, Substitution, cache_readonly

from pandas.core.dtypes.common import (
ensure_platform_int,
Expand All @@ -27,6 +27,7 @@
from pandas.core import accessor
from pandas.core.algorithms import take_1d
from pandas.core.arrays.categorical import Categorical, _recode_for_categories, contains
from pandas.core.base import _shared_docs
import pandas.core.common as com
import pandas.core.indexes.base as ibase
from pandas.core.indexes.base import Index, _index_shared_docs
Expand Down Expand Up @@ -555,6 +556,11 @@ def _can_reindex(self, indexer):
""" always allow reindexing """
pass

@Substitution(klass="CategoricalIndex")
@Appender(_shared_docs["searchsorted"])
def searchsorted(self, value, side="left", sorter=None):
return self._data.searchsorted(value, side=side, sorter=sorter)

@Appender(_index_shared_docs["where"])
def where(self, cond, other=None):
# TODO: Investigate an alternative implementation with
Expand Down
9 changes: 4 additions & 5 deletions pandas/tests/arrays/categorical/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,15 @@ def test_searchsorted(self, ordered_fixture):
tm.assert_numpy_array_equal(res_ser, exp)

# Searching for a single value that is not from the Categorical
msg = r"Value\(s\) to be inserted must be in categories"
with pytest.raises(KeyError, match=msg):
with pytest.raises(KeyError, match="cucumber"):
cat.searchsorted("cucumber")
with pytest.raises(KeyError, match=msg):
with pytest.raises(KeyError, match="cucumber"):
ser.searchsorted("cucumber")

# Searching for multiple values one of each is not from the Categorical
with pytest.raises(KeyError, match=msg):
with pytest.raises(KeyError, match="cucumber"):
cat.searchsorted(["bread", "cucumber"])
with pytest.raises(KeyError, match=msg):
with pytest.raises(KeyError, match="cucumber"):
ser.searchsorted(["bread", "cucumber"])

def test_unique(self):
Expand Down