Skip to content

PERF: faster categorical ops for equal or larger than scalar #29820

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
Nov 25, 2019
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: 3 additions & 1 deletion doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,9 @@ Performance improvements
- 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`)
- Performance improvement when comparing a :meth:`Categorical` with a scalar and the scalar is not found in the categories (:issue:`29750`)
- Performance improvement when comparing a :class:`Categorical` with a scalar and the scalar is not found in the categories (:issue:`29750`)
- Performance improvement when checking if values in a :class:`Categorical` are equal, equal or larger or larger than a given scalar.
The improvement is not present if checking if the :class:`Categorical` is less than or less than or equal than the scalar (:issue:`xxxxx`)
Copy link
Member

Choose a reason for hiding this comment

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

Why don't we have an issue number here? Otherwise, we should use the PR number.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, added.


.. _whatsnew_1000.bug_fixes:

Expand Down
9 changes: 5 additions & 4 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ def func(self, other):
else:
other_codes = other._codes

mask = (self._codes == -1) | (other_codes == -1)
f = getattr(self._codes, opname)
ret = f(other_codes)
mask = (self._codes == -1) | (other_codes == -1)
if mask.any():
# In other series, the leads to False, so do that here too
ret[mask] = False
Expand All @@ -121,9 +121,10 @@ def func(self, other):
i = self.categories.get_loc(other)
ret = getattr(self._codes, opname)(i)

# check for NaN in self
mask = self._codes == -1
ret[mask] = False
if opname not in {"eq", "__eq__", "ge", "__ge__", "gt", "__gt__"}:
Copy link
Contributor

Choose a reason for hiding this comment

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

this is very strange that you are checking for eq and eq it’s just eq

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, changed.

# check for NaN needed if we are not equal or larger
mask = self._codes == -1
ret[mask] = False
Copy link
Member

Choose a reason for hiding this comment

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

Do we have a performance test for this?

Copy link
Contributor Author

@topper-123 topper-123 Nov 24, 2019

Choose a reason for hiding this comment

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

No, no ASV's for this ATM. I actually can't get ASV to run locally, maybe a Windows issue?

Anyway, I've added I've a ASV test set, but haven't been able to run it myself, unforfunately. Isn't there a web page, where we post ASVs?

return ret
else:
if opname == "__eq__":
Expand Down