Skip to content

Commit 8d856e1

Browse files
authored
Backport PR #42268: PERF: IntervalIndex.intersection (#42283)
1 parent 25326ce commit 8d856e1

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

pandas/core/indexes/base.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -3099,9 +3099,13 @@ def _intersection(self, other: Index, sort=False):
30993099
"""
31003100
intersection specialized to the case with matching dtypes.
31013101
"""
3102-
# TODO(EA): setops-refactor, clean all this up
3103-
3104-
if self.is_monotonic and other.is_monotonic:
3102+
if (
3103+
self.is_monotonic
3104+
and other.is_monotonic
3105+
and not is_interval_dtype(self.dtype)
3106+
):
3107+
# For IntervalIndex _inner_indexer is not more performant than get_indexer,
3108+
# so don't take this fastpath
31053109
try:
31063110
result = self._inner_indexer(other)[0]
31073111
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,
@@ -801,6 +802,19 @@ def _is_all_dates(self) -> bool:
801802
"""
802803
return False
803804

805+
def _get_join_target(self) -> np.ndarray:
806+
# constructing tuples is much faster than constructing Intervals
807+
tups = list(zip(self.left, self.right))
808+
target = construct_1d_object_array_from_listlike(tups)
809+
return target
810+
811+
def _from_join_target(self, result):
812+
left, right = list(zip(*result))
813+
arr = type(self._data).from_arrays(
814+
left, right, dtype=self.dtype, closed=self.closed
815+
)
816+
return type(self)._simple_new(arr, name=self.name)
817+
804818
# TODO: arithmetic operations
805819

806820

0 commit comments

Comments
 (0)