Skip to content

PERF: avoid unneeded recoding of categoricals and reuse CategoricalDtypes for greater slicing speed #21659

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 2 commits into from
Jun 29, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
35 changes: 35 additions & 0 deletions asv_bench/benchmarks/categoricals.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,38 @@ def time_categorical_index_contains(self):

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


class CategoricalSlicing(object):

goal_time = 0.2
params = ['monotonic_incr', 'monotonic_decr', 'non_monotonic']
param_names = ['index']

def setup(self, index):
N = 10**6
values = list('a' * N + 'b' * N + 'c' * N)
indices = {
'monotonic_incr': pd.Categorical(values),
'monotonic_decr': pd.Categorical(reversed(values)),
'non_monotonic': pd.Categorical(list('abc' * N))}
self.data = indices[index]

self.scalar = 10000
self.list = list(range(10000))
self.cat_scalar = 'b'

def time_getitem_scalar(self, index):
self.data[self.scalar]

def time_getitem_slice(self, index):
self.data[:self.scalar]

def time_getitem_list_like(self, index):
self.data[[self.scalar]]

def time_getitem_list(self, index):
self.data[self.list]

def time_getitem_bool_array(self, index):
self.data[self.data == self.cat_scalar]
46 changes: 45 additions & 1 deletion asv_bench/benchmarks/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import numpy as np
import pandas.util.testing as tm
from pandas import (Series, DataFrame, MultiIndex, Int64Index, Float64Index,
IntervalIndex, IndexSlice, concat, date_range)
IntervalIndex, CategoricalIndex,
IndexSlice, concat, date_range)
from .pandas_vb_common import setup, Panel # noqa


Expand Down Expand Up @@ -230,6 +231,49 @@ def time_loc_list(self, monotonic):
monotonic.loc[80000:]


class CategoricalIndexIndexing(object):

goal_time = 0.2
params = ['monotonic_incr', 'monotonic_decr', 'non_monotonic']
param_names = ['index']

def setup(self, index):
N = 10**5
values = list('a' * N + 'b' * N + 'c' * N)
indices = {
'monotonic_incr': CategoricalIndex(values),
'monotonic_decr': CategoricalIndex(reversed(values)),
'non_monotonic': CategoricalIndex(list('abc' * N))}
self.data = indices[index]

self.int_scalar = 10000
self.int_list = list(range(10000))

self.cat_scalar = 'b'
self.cat_list = ['a', 'c']

def time_getitem_scalar(self, index):
self.data[self.int_scalar]

def time_getitem_slice(self, index):
self.data[:self.int_scalar]

def time_getitem_list_like(self, index):
self.data[[self.int_scalar]]

def time_getitem_list(self, index):
self.data[self.int_list]

def time_getitem_bool_array(self, index):
self.data[self.data == self.cat_scalar]

def time_get_loc_scalar(self, index):
self.data.get_loc(self.cat_scalar)

def time_get_indexer_list(self, index):
self.data.get_indexer(self.cat_list)


class PanelIndexing(object):

goal_time = 0.2
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -2009,8 +2009,7 @@ def __getitem__(self, key):
return self.categories[i]
else:
return self._constructor(values=self._codes[key],
categories=self.categories,
ordered=self.ordered, fastpath=True)
dtype=self.dtype, fastpath=True)

def __setitem__(self, key, value):
""" Item assignment.
Expand Down
13 changes: 9 additions & 4 deletions pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,20 @@ def __eq__(self, other):
"""
Rules for CDT equality:
1) Any CDT is equal to the string 'category'
2) Any CDT is equal to a CDT with categories=None regardless of ordered
3) A CDT with ordered=True is only equal to another CDT with
2) Any CDT is equal to itself
3) Any CDT is equal to a CDT with categories=None regardless of ordered
4) A CDT with ordered=True is only equal to another CDT with
ordered=True and identical categories in the same order
4) A CDT with ordered={False, None} is only equal to another CDT with
5) A CDT with ordered={False, None} is only equal to another CDT with
ordered={False, None} and identical categories, but same order is
not required. There is no distinction between False/None.
5) Any other comparison returns False
6) Any other comparison returns False
"""
if isinstance(other, compat.string_types):
return other == self.name

if other is self:
return True
Copy link
Contributor

Choose a reason for hiding this comment

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

prob doesn't make any difference, but this could be elif (existing if)

if not (hasattr(other, 'ordered') and hasattr(other, 'categories')):
return False
elif self.categories is None or other.categories is None:
Expand Down Expand Up @@ -348,6 +351,8 @@ def update_dtype(self, dtype):
msg = ('a CategoricalDtype must be passed to perform an update, '
'got {dtype!r}').format(dtype=dtype)
raise ValueError(msg)
elif dtype.categories is not None and dtype.ordered is self.ordered:
return dtype

# dtype is CDT: keep current categories/ordered if None
new_categories = dtype.categories
Expand Down
7 changes: 3 additions & 4 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def _create_categorical(cls, data, categories=None, ordered=None,
data = data.set_categories(categories, ordered=ordered)
elif ordered is not None and ordered != data.ordered:
data = data.set_ordered(ordered)
if isinstance(dtype, CategoricalDtype):
if isinstance(dtype, CategoricalDtype) and dtype != data.dtype:
Copy link
Contributor Author

@topper-123 topper-123 Jun 27, 2018

Choose a reason for hiding this comment

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

It should be noted that in master this dtype comparison is quite slow:

>>> d = pd.api.types.CategoricalDtype(categories=['a', 'b', 'c'])
>>> %timeit d == d
149 µs  # master
524 ns  # this PR

This is the reason for the focus on improving dtype comparisons also in this PR, so this check doesn't cause slowdowns on other parts of pandas. Otherwise the performance benefits of this PR would be ambivalent, causing some slowdowns also).

# we want to silently ignore dtype='category'
data = data._set_dtype(dtype)
return data
Expand Down Expand Up @@ -236,7 +236,7 @@ def _is_dtype_compat(self, other):
if not is_list_like(values):
values = [values]
other = CategoricalIndex(self._create_categorical(
other, categories=self.categories, ordered=self.ordered))
other, dtype=self.dtype))
if not other.isin(values).all():
raise TypeError("cannot append a non-category item to a "
"CategoricalIndex")
Expand Down Expand Up @@ -798,8 +798,7 @@ def _evaluate_compare(self, other):
other = other._values
elif isinstance(other, Index):
other = self._create_categorical(
other._values, categories=self.categories,
ordered=self.ordered)
other._values, dtype=self.dtype)

if isinstance(other, (ABCCategorical, np.ndarray,
ABCSeries)):
Expand Down