Skip to content

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions doc/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,7 @@ Attributes
Index.is_monotonic_increasing
Index.is_monotonic_decreasing
Index.is_unique
Index.has_duplicates
Index.dtype
Index.inferred_type
Index.is_all_dates
Expand Down
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.16.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,5 @@ Bug Fixes
~~~~~~~~~

.. _whatsnew_0160.bug_fixes:

- Bug in ``MultiIndex.has_duplicates`` when having many levels causes an indexer overflow (:issue:`9075`)
15 changes: 13 additions & 2 deletions pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']

Expand Down Expand Up @@ -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()
Copy link
Contributor

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 that has_duplicates does not need compressed ids and -1 labels should be handled differently.

Copy link
Contributor Author

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?

Copy link
Contributor

Choose a reason for hiding this comment

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

ok, will do


# 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
Copy link
Contributor

Choose a reason for hiding this comment

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

this will break if any of the self.labels[i] are -1.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

so if their any NaN's, what would you do?

Copy link
Contributor

Choose a reason for hiding this comment

The 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 _maybe_lift function which is part of this PR.


if len(np.unique(group_index)) < len(group_index):
return True
Expand Down
3 changes: 3 additions & 0 deletions pandas/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,9 @@ def test_duplicated_drop_duplicates(self):
tm.assert_index_equal(result, original)
self.assertFalse(result is original)

# has_duplicates
self.assertFalse(original.has_duplicates)

# create repeated values, 3rd and 5th values are duplicated
idx = original[list(range(len(original))) + [5, 3]]
expected = Index([False] * len(original) + [True, True])
Expand Down
43 changes: 43 additions & 0 deletions pandas/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -3435,6 +3435,49 @@ def test_has_duplicates(self):
[0, 1, 2, 0, 0, 1, 2]])
self.assertTrue(index.has_duplicates)

# GH 9075
t = [(u'x', u'out', u'z', 5, u'y', u'in', u'z', 169),
(u'x', u'out', u'z', 7, u'y', u'in', u'z', 119),
(u'x', u'out', u'z', 9, u'y', u'in', u'z', 135),
(u'x', u'out', u'z', 13, u'y', u'in', u'z', 145),
(u'x', u'out', u'z', 14, u'y', u'in', u'z', 158),
(u'x', u'out', u'z', 16, u'y', u'in', u'z', 122),
(u'x', u'out', u'z', 17, u'y', u'in', u'z', 160),
(u'x', u'out', u'z', 18, u'y', u'in', u'z', 180),
(u'x', u'out', u'z', 20, u'y', u'in', u'z', 143),
(u'x', u'out', u'z', 21, u'y', u'in', u'z', 128),
(u'x', u'out', u'z', 22, u'y', u'in', u'z', 129),
(u'x', u'out', u'z', 25, u'y', u'in', u'z', 111),
(u'x', u'out', u'z', 28, u'y', u'in', u'z', 114),
(u'x', u'out', u'z', 29, u'y', u'in', u'z', 121),
(u'x', u'out', u'z', 31, u'y', u'in', u'z', 126),
(u'x', u'out', u'z', 32, u'y', u'in', u'z', 155),
(u'x', u'out', u'z', 33, u'y', u'in', u'z', 123),
(u'x', u'out', u'z', 12, u'y', u'in', u'z', 144)]
index = pd.MultiIndex.from_tuples(t)
self.assertFalse(index.has_duplicates)

# handle int64 overflow if possible
def check(nlevels):
labels = np.tile(np.arange(500), 2)
level = np.arange(500)

# no dups
index = MultiIndex(levels=[level] * nlevels + [[0, 1]],
labels=[labels] * nlevels + [np.arange(2).repeat(500)])
self.assertFalse(index.has_duplicates)

# with a dup
values = index.values.tolist()
index = MultiIndex.from_tuples(values + [values[0]])
self.assertTrue(index.has_duplicates)

# no overflow
check(4)

# overflow possible
check(8)

def test_tolist(self):
result = self.index.tolist()
exp = list(self.index.values)
Expand Down