Skip to content

Commit 998a0de

Browse files
makbigcjschendel
authored andcommitted
[PERF] Get rid of MultiIndex conversion in IntervalIndex.is_unique (#26391)
1 parent 19f693f commit 998a0de

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

asv_bench/benchmarks/index_object.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def time_get_loc(self):
183183

184184
class IntervalIndexMethod:
185185
# GH 24813
186-
params = [10**3, 10**5]
186+
params = [10**3, 10**5, 10**7]
187187

188188
def setup(self, N):
189189
left = np.append(np.arange(N), np.array(0))
@@ -194,5 +194,8 @@ def setup(self, N):
194194
def time_monotonic_inc(self, N):
195195
self.intv.is_monotonic_increasing
196196

197+
def time_is_unique(self, N):
198+
self.intv.is_unique
199+
197200

198201
from .pandas_vb_common import setup # noqa: F401

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ Performance Improvements
336336
- Improved performance of :meth:`Series.map` for dictionary mappers on categorical series by mapping the categories instead of mapping all values (:issue:`23785`)
337337
- Improved performance of :meth:`read_csv` by faster concatenating date columns without extra conversion to string for integer/float zero
338338
and float NaN; by faster checking the string for the possibility of being a date (:issue:`25754`)
339+
- Improved performance of :meth:`IntervalIndex.is_unique` by removing conversion to `MultiIndex` (:issue:`24813`)
339340

340341
.. _whatsnew_0250.bug_fixes:
341342

pandas/core/indexes/interval.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,24 @@ def is_unique(self):
461461
"""
462462
Return True if the IntervalIndex contains unique elements, else False
463463
"""
464-
return self._multiindex.is_unique
464+
left = self.left
465+
right = self.right
466+
467+
if self.isna().sum() > 1:
468+
return False
469+
470+
if left.is_unique or right.is_unique:
471+
return True
472+
473+
seen_pairs = set()
474+
check_idx = np.where(left.duplicated(keep=False))[0]
475+
for idx in check_idx:
476+
pair = (left[idx], right[idx])
477+
if pair in seen_pairs:
478+
return False
479+
seen_pairs.add(pair)
480+
481+
return True
465482

466483
@cache_readonly
467484
@Appender(_interval_shared_docs['is_non_overlapping_monotonic']

0 commit comments

Comments
 (0)