Skip to content

Commit 9e29eb8

Browse files
topper-123ukarroum
authored andcommitted
PERF: faster numeric indexes comparisons when identical (pandas-dev#37569)
1 parent efc4f70 commit 9e29eb8

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

pandas/core/indexes/numeric.py

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import operator
12
from typing import Any
23

34
import numpy as np
@@ -185,6 +186,18 @@ def _union(self, other, sort):
185186
else:
186187
return super()._union(other, sort)
187188

189+
def _cmp_method(self, other, op):
190+
if self.is_(other): # fastpath
191+
if op in {operator.eq, operator.le, operator.ge}:
192+
arr = np.ones(len(self), dtype=bool)
193+
if self._can_hold_na:
194+
arr[self.isna()] = False
195+
return arr
196+
elif op in {operator.ne, operator.lt, operator.gt}:
197+
return np.zeros(len(self), dtype=bool)
198+
199+
return super()._cmp_method(other, op)
200+
188201

189202
_num_index_shared_docs[
190203
"class_descr"

pandas/core/indexes/range.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -813,11 +813,8 @@ def any(self, *args, **kwargs) -> bool:
813813

814814
def _cmp_method(self, other, op):
815815
if isinstance(other, RangeIndex) and self._range == other._range:
816-
if op in {operator.eq, operator.le, operator.ge}:
817-
return np.ones(len(self), dtype=bool)
818-
elif op in {operator.ne, operator.lt, operator.gt}:
819-
return np.zeros(len(self), dtype=bool)
820-
816+
# Both are immutable so if ._range attr. are equal, shortcut is possible
817+
return super()._cmp_method(self, op)
821818
return super()._cmp_method(other, op)
822819

823820
def _arith_method(self, other, op):

0 commit comments

Comments
 (0)