Skip to content

Backport PR #42334 on branch 1.3.x (PERF/REGR: restore IntervalIndex._intersection_non_unique) #42336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,8 @@ def _intersection(self, other, sort):
# multiple NaNs
taken = other._intersection_unique(self)
else:
return super()._intersection(other, sort)
# duplicates
taken = self._intersection_non_unique(other)

if sort is None:
taken = taken.sort_values()
Expand Down Expand Up @@ -837,6 +838,35 @@ def _intersection_unique(self, other: IntervalIndex) -> IntervalIndex:

return self.take(indexer)

def _intersection_non_unique(self, other: IntervalIndex) -> IntervalIndex:
"""
Used when the IntervalIndex does have some common endpoints,
on either sides.
Return the intersection with another IntervalIndex.

Parameters
----------
other : IntervalIndex

Returns
-------
IntervalIndex
"""
# Note: this is about 3.25x faster than super()._intersection(other)
# in IntervalIndexMethod.time_intersection_both_duplicate(1000)
mask = np.zeros(len(self), dtype=bool)

if self.hasnans and other.hasnans:
first_nan_loc = np.arange(len(self))[self.isna()][0]
mask[first_nan_loc] = True

other_tups = set(zip(other.left, other.right))
for i, tup in enumerate(zip(self.left, self.right)):
if tup in other_tups:
mask[i] = True

return self[mask]

# --------------------------------------------------------------------

@property
Expand Down