Skip to content

Commit 534afbc

Browse files
committed
PERF: Faster comparisons of indexes when the same
1 parent c0c3516 commit 534afbc

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

pandas/core/indexes/base.py

+18
Original file line numberDiff line numberDiff line change
@@ -5363,6 +5363,24 @@ def _cmp_method(self, other, op):
53635363
"""
53645364
Wrapper used to dispatch comparison operations.
53655365
"""
5366+
from .multi import MultiIndex
5367+
5368+
if self.is_(other) and not isinstance(self, MultiIndex):
5369+
# short-circuit when other is same as self
5370+
# if we're comparing equality, return an np.ones array, else an np.zeros arr
5371+
bool_arr_type, na_bool = {
5372+
operator.eq: (np.ones, False),
5373+
operator.le: (np.ones, False),
5374+
operator.ge: (np.ones, False),
5375+
operator.ne: (np.zeros, True),
5376+
operator.lt: (np.zeros, True),
5377+
operator.gt: (np.zeros, True),
5378+
}[op]
5379+
bool_arr = bool_arr_type(self.shape, dtype=bool)
5380+
if self._can_hold_na:
5381+
bool_arr[isna(self)] = na_bool
5382+
return bool_arr
5383+
53665384
if isinstance(other, (np.ndarray, Index, ABCSeries, ExtensionArray)):
53675385
if len(self) != len(other):
53685386
raise ValueError("Lengths must match to compare")

0 commit comments

Comments
 (0)