Skip to content

Commit 569a615

Browse files
authored
PERF: IntervalIndex.intersection (#42268)
1 parent e6b57b9 commit 569a615

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

pandas/core/indexes/base.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -3089,7 +3089,13 @@ def _intersection(self, other: Index, sort=False):
30893089
"""
30903090
intersection specialized to the case with matching dtypes.
30913091
"""
3092-
if self.is_monotonic and other.is_monotonic:
3092+
if (
3093+
self.is_monotonic
3094+
and other.is_monotonic
3095+
and not is_interval_dtype(self.dtype)
3096+
):
3097+
# For IntervalIndex _inner_indexer is not more performant than get_indexer,
3098+
# so don't take this fastpath
30933099
try:
30943100
result = self._inner_indexer(other)[0]
30953101
except TypeError:

pandas/core/indexes/interval.py

+14
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from pandas.util._exceptions import rewrite_exception
3838

3939
from pandas.core.dtypes.cast import (
40+
construct_1d_object_array_from_listlike,
4041
find_common_type,
4142
infer_dtype_from_scalar,
4243
maybe_box_datetimelike,
@@ -796,6 +797,19 @@ def _is_all_dates(self) -> bool:
796797
"""
797798
return False
798799

800+
def _get_join_target(self) -> np.ndarray:
801+
# constructing tuples is much faster than constructing Intervals
802+
tups = list(zip(self.left, self.right))
803+
target = construct_1d_object_array_from_listlike(tups)
804+
return target
805+
806+
def _from_join_target(self, result):
807+
left, right = list(zip(*result))
808+
arr = type(self._data).from_arrays(
809+
left, right, dtype=self.dtype, closed=self.closed
810+
)
811+
return type(self)._simple_new(arr, name=self.name)
812+
799813
# TODO: arithmetic operations
800814

801815

0 commit comments

Comments
 (0)