Skip to content

PERF: speed-up when scalar not found in Categorical's categories #29750

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
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
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 @@ -343,6 +343,7 @@ 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 searching for a scalar in a :meth:`Categorical` and the scalar is not found in the categories (:issue:`29750`)

.. _whatsnew_1000.bug_fixes:

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ def f(self, other):
return ret
else:
if opname == "__eq__":
return np.repeat(False, len(self))
return np.zeros(len(self), dtype=bool)
elif opname == "__ne__":
return np.repeat(True, len(self))
return np.ones(len(self), dtype=bool)
else:
msg = (
"Cannot compare a Categorical for op {op} with a "
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/arrays/categorical/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_comparisons(self):
tm.assert_numpy_array_equal(result, expected)

result = self.factor == "d"
expected = np.repeat(False, len(self.factor))
expected = np.zeros(len(self.factor), dtype=bool)
tm.assert_numpy_array_equal(result, expected)

# comparisons with categoricals
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/interval/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ def test_with_nans(self, closed):
assert index.hasnans is False

result = index.isna()
expected = np.repeat(False, len(index))
expected = np.zeros(len(index), dtype=bool)
tm.assert_numpy_array_equal(result, expected)

result = index.notna()
expected = np.repeat(True, len(index))
expected = np.ones(len(index), dtype=bool)
tm.assert_numpy_array_equal(result, expected)

index = self.create_index_with_nan(closed=closed)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ def test_nanosecond_index_access(self):
assert first_value == x[Timestamp(expected_ts)]

def test_booleanindex(self, index):
bool_index = np.repeat(True, len(index)).astype(bool)
bool_index = np.ones(len(index), dtype=bool)
bool_index[5:30:2] = False

sub_index = index[bool_index]
Expand Down