Skip to content

PERF: Fixed cut regression, improve Categorical #34952

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 4 commits into from
Jun 24, 2020
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
4 changes: 4 additions & 0 deletions asv_bench/benchmarks/categoricals.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def setup(self):
self.values_all_int8 = np.ones(N, "int8")
self.categorical = pd.Categorical(self.values, self.categories)
self.series = pd.Series(self.categorical)
self.intervals = pd.interval_range(0, 1, periods=N // 10)

def time_regular(self):
pd.Categorical(self.values, self.categories)
Expand All @@ -44,6 +45,9 @@ def time_fastpath(self):
def time_datetimes(self):
pd.Categorical(self.datetimes)

def time_interval(self):
pd.Categorical(self.datetimes, categories=self.datetimes)

def time_datetimes_with_nat(self):
pd.Categorical(self.datetimes_with_nat)

Expand Down
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,8 @@ Performance improvements
- Performance improvement for groupby methods :meth:`~pandas.core.groupby.groupby.Groupby.first`
and :meth:`~pandas.core.groupby.groupby.Groupby.last` (:issue:`34178`)
- Performance improvement in :func:`factorize` for nullable (integer and boolean) dtypes (:issue:`33064`).
- Performance improvement when constructing :class`Categorical` objects (:issue:`33921`)
- Fixed performance regression in :func:`pandas.qcut` and :func:`pandas.cut` (:issue:`33921`)
- Performance improvement in reductions (sum, prod, min, max) for nullable (integer and boolean) dtypes (:issue:`30982`, :issue:`33261`, :issue:`33442`).
- Performance improvement in arithmetic operations between two :class:`DataFrame` objects (:issue:`32779`)
- Performance improvement in :class:`pandas.core.groupby.RollingGroupby` (:issue:`34052`)
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -2611,6 +2611,11 @@ def _get_codes_for_values(values, categories):
values = ensure_object(values)
categories = ensure_object(categories)

if isinstance(categories, ABCIndexClass):
Copy link
Contributor

Choose a reason for hiding this comment

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

hmm shouldn't this be in the caller to this routine?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's best here. We call it in ~3 places, and I think each would have to repeat this check & the object casting stuff.

return coerce_indexer_dtype(categories.get_indexer(values), categories)

# Only hit here when we've already coerced to object dtypee.

hash_klass, vals = _get_data_algo(values)
_, cats = _get_data_algo(categories)
t = hash_klass(len(cats))
Expand Down
15 changes: 10 additions & 5 deletions pandas/core/reshape/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ def _bins_to_cuts(

na_mask = isna(x) | (ids == len(bins)) | (ids == 0)
has_nas = na_mask.any()
labels_are_unique = None

if labels is not False:
if not (labels is None or is_list_like(labels)):
Expand All @@ -425,6 +426,7 @@ def _bins_to_cuts(
labels = _format_labels(
bins, precision, right=right, include_lowest=include_lowest, dtype=dtype
)
labels_are_unique = True
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looking again, it may not be safe to assume that _format_labels returns a unique IntervalIndex like I thought. Might revert this for now.

elif ordered and len(set(labels)) != len(labels):
raise ValueError(
"labels must be unique if ordered=True; pass ordered=False for duplicate labels" # noqa
Expand All @@ -435,11 +437,14 @@ def _bins_to_cuts(
"Bin labels must be one fewer than the number of bin edges"
)
if not is_categorical_dtype(labels):
labels = Categorical(
labels,
categories=labels if len(set(labels)) == len(labels) else None,
ordered=ordered,
)
if labels_are_unique:
Copy link
Contributor

Choose a reason for hiding this comment

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

why this extra logic?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry... I thought I reverted this. Forgot to push.

But, the set(labels) stuff is somewhat expensive (don't have the exact numbers right now). There may be cases where we know that we have unique labels and can avoid the check.

categories = labels
elif labels_are_unique is None:
categories = labels if len(set(labels)) == len(labels) else None
else:
categories = None

labels = Categorical(labels, categories=categories, ordered=ordered)
# TODO: handle mismatch between categorical label order and pandas.cut order.
np.putmask(ids, na_mask, 0)
result = algos.take_nd(labels, ids - 1)
Expand Down
35 changes: 35 additions & 0 deletions pandas/tests/arrays/categorical/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,3 +643,38 @@ def test_constructor_string_and_tuples(self):
c = pd.Categorical(np.array(["c", ("a", "b"), ("b", "a"), "c"], dtype=object))
expected_index = pd.Index([("a", "b"), ("b", "a"), "c"])
assert c.categories.equals(expected_index)

def test_interval(self):
idx = pd.interval_range(0, 10, periods=10)
cat = pd.Categorical(idx, categories=idx)
expected_codes = np.arange(10, dtype="int8")
tm.assert_numpy_array_equal(cat.codes, expected_codes)
tm.assert_index_equal(cat.categories, idx)

# infer categories
cat = pd.Categorical(idx)
tm.assert_numpy_array_equal(cat.codes, expected_codes)
tm.assert_index_equal(cat.categories, idx)

# list values
cat = pd.Categorical(list(idx))
tm.assert_numpy_array_equal(cat.codes, expected_codes)
tm.assert_index_equal(cat.categories, idx)

# list values, categories
cat = pd.Categorical(list(idx), categories=list(idx))
tm.assert_numpy_array_equal(cat.codes, expected_codes)
tm.assert_index_equal(cat.categories, idx)

# shuffled
values = idx.take([1, 2, 0])
cat = pd.Categorical(values, categories=idx)
tm.assert_numpy_array_equal(cat.codes, np.array([1, 2, 0], dtype="int8"))
tm.assert_index_equal(cat.categories, idx)

# extra
values = pd.interval_range(8, 11, periods=3)
cat = pd.Categorical(values, categories=idx)
expected_codes = np.array([8, 9, -1], dtype="int8")
tm.assert_numpy_array_equal(cat.codes, expected_codes)
tm.assert_index_equal(cat.categories, idx)