-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
Get rid of MultiIndex conversion in IntervalIndex.is_unique #25159
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| import numpy as np | ||
| import pandas.util.testing as tm | ||
| from pandas import (Series, date_range, DatetimeIndex, Index, RangeIndex, | ||
| Float64Index) | ||
| Float64Index, IntervalIndex) | ||
|
|
||
|
|
||
| class SetOperations(object): | ||
|
|
@@ -181,4 +181,16 @@ def time_get_loc(self): | |
| self.ind.get_loc(0) | ||
|
|
||
|
|
||
| class IntervalIndexMethod(object): | ||
| # GH 24813 | ||
| def setup(self): | ||
| N = 10**5 | ||
| left = np.append(np.arange(N), np.array(0)) | ||
| right = np.append(np.arange(1, N + 1), np.array(1)) | ||
| self.intv = IntervalIndex.from_arrays(left, right) | ||
|
|
||
| def time_is_unique(self): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add another benchmark with N**3, e.g. s small case |
||
| self.intv.is_unique | ||
|
|
||
|
|
||
| from .pandas_vb_common import setup # noqa: F401 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -463,7 +463,13 @@ def is_unique(self): | |
| """ | ||
| Return True if the IntervalIndex contains unique elements, else False | ||
| """ | ||
| return self._multiindex.is_unique | ||
| left = self.values.left | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why isnt the answer just:
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a little too strict; you don't necessarily need left or right uniqueness, only pairwise uniqueness, as you can have duplicate endpoints on one side as long as the other side also isn't the same, e.g.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think u can simply construct a numpy array and check that then via np.unique by first reshaping
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have tried something below But np.unique cannot filter out the duplicate np.nan I'm still looking for other way.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use pd.unique |
||
| right = self.values.right | ||
| for i in range(len(self)): | ||
| mask = (left[i] == left) & (right[i] == right) | ||
| if mask.sum() > 1: | ||
| return False | ||
| return True | ||
|
|
||
| @cache_readonly | ||
| @Appender(_interval_shared_docs['is_non_overlapping_monotonic'] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this where we asv on is_unique?