Skip to content

[PERF] Get rid of MultiIndex conversion in IntervalIndex.is_unique #26391

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
merged 7 commits into from
May 28, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 3 additions & 0 deletions asv_bench/benchmarks/index_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,5 +194,8 @@ def setup(self, N):
def time_monotonic_inc(self, N):
self.intv.is_monotonic_increasing

def time_is_unique(self, N):
self.intv.is_unique


from .pandas_vb_common import setup # noqa: F401
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ Performance Improvements
- Improved performance of :meth:`Series.map` for dictionary mappers on categorical series by mapping the categories instead of mapping all values (:issue:`23785`)
- Improved performance of :meth:`read_csv` by faster concatenating date columns without extra conversion to string for integer/float zero
and float NaN; by faster checking the string for the possibility of being a date (:issue:`25754`)
- Improved performance of :meth:`IntervalIndex.is_unique` by removing conversion to `MultiIndex` (:issue:`24813`)

.. _whatsnew_0250.bug_fixes:

Expand Down
20 changes: 19 additions & 1 deletion pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" define the IntervalIndex """
from collections import defaultdict
import textwrap
import warnings

Expand Down Expand Up @@ -461,7 +462,24 @@ def is_unique(self):
"""
Return True if the IntervalIndex contains unique elements, else False
"""
return self._multiindex.is_unique
left = self.left
right = self.right

if self.isna().sum() > 1:
return False

if left.is_unique or right.is_unique:
return True

seen_pairs = defaultdict(bool)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you just use a set?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, yes, that's a lot cleaner - originally was treating endpoints separately and needed the dict structure but should be unnecessary when dealing with tuples

check_idx = np.where(left.duplicated(keep=False))[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

acutally can't you just do this

pairs = [(left[idx], right[idx] for idx in checks_idx]
return len(set(pairs)) == len(pairs)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The present approach may be better in which False is returned once a duplicate is found.
To compare the length, we run over all potential duplicates.

for idx in check_idx:
pair = (left[idx], right[idx])
if seen_pairs[pair]:
return False
seen_pairs[pair] = True

return True

@cache_readonly
@Appender(_interval_shared_docs['is_non_overlapping_monotonic']
Expand Down