-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Bug in MultiIndex.has_duplicates when having many levels causes an indexer overflow (GH9075) #9077
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
BUG: Bug in MultiIndex.has_duplicates when having many levels causes an indexer overflow (GH9075) #9077
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 |
---|---|---|
|
@@ -600,6 +600,10 @@ def is_unique(self): | |
""" return if the index has unique values """ | ||
return self._engine.is_unique | ||
|
||
@property | ||
def has_duplicates(self): | ||
return not self.is_unique | ||
|
||
def is_boolean(self): | ||
return self.inferred_type in ['boolean'] | ||
|
||
|
@@ -3223,12 +3227,19 @@ def has_duplicates(self): | |
""" | ||
Return True if there are no unique groups | ||
""" | ||
# has duplicates | ||
|
||
from pandas.core.groupby import _int64_overflow_possible | ||
|
||
# if we have a possible overflow, then fallback to safe method | ||
shape = [len(lev) for lev in self.levels] | ||
if _int64_overflow_possible(shape): | ||
return self.duplicated().any() | ||
|
||
# int64 capable | ||
group_index = np.zeros(len(self), dtype='i8') | ||
for i in range(len(shape)): | ||
stride = np.prod([x for x in shape[i + 1:]], dtype='i8') | ||
group_index += self.labels[i] * stride | ||
group_index += _ensure_int64(self.labels[i]) * stride | ||
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. this will break if any of the 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. so if their any NaN's, what would you do? 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. if any of the labels are -1, we just need to lift labels and size by one, just as in |
||
|
||
if len(np.unique(group_index)) < len(group_index): | ||
return True | ||
|
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.
given that we have integer labels, I think it will be more efficient if we keep working with the labels.
I particularly like how
get_compressed_ids
handles this. The difference here is thathas_duplicates
does not need compressed ids and-1
labels should be handled differently.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.
ok, since you are pretty familiar with this, want to give it a whirl?
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.
ok, will do