Skip to content

Commit

Permalink
PERF: speed-up when scalar not found in Categorical's categories (#29750
Browse files Browse the repository at this point in the history
)
  • Loading branch information
topper-123 authored and jreback committed Nov 21, 2019
1 parent c0050e0 commit 2570c1d
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 6 deletions.
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 @@ -357,6 +357,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 func(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:
raise TypeError(
f"Cannot compare a Categorical for op {opname} 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

0 comments on commit 2570c1d

Please sign in to comment.