Skip to content

BUG in MultiIndex.drop for not-lexsorted multi-indexes, #12078 #12158

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
4 changes: 2 additions & 2 deletions doc/source/whatsnew/v0.18.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ Bug Fixes
- Bug in ``read_sql`` with ``pymysql`` connections failing to return chunked data (:issue:`11522`)
- Bug in ``.to_csv`` ignoring formatting parameters ``decimal``, ``na_rep``, ``float_format`` for float indexes (:issue:`11553`)
- Bug in ``Int64Index`` and ``Float64Index`` preventing the use of the modulo operator (:issue:`9244`)

- Bug in ``MultiIndex.drop`` for not lexsorted multi-indexes (:issue:`12078`)

- Bug in ``DataFrame`` when masking an empty ``DataFrame`` (:issue:`11859`)

Expand All @@ -544,4 +544,4 @@ Bug Fixes

- Bug in ``.skew`` and ``.kurt`` due to roundoff error for highly similar values (:issue:`11974`)

- Bug in ``buffer_rd_bytes`` src->buffer could be freed more than once if reading failed, causing a segfault (:issue:`12098`)
- Bug in ``buffer_rd_bytes`` src->buffer could be freed more than once if reading failed, causing a segfault (:issue:`12098`)
16 changes: 15 additions & 1 deletion pandas/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1083,10 +1083,24 @@ def drop(self, labels, level=None, errors='raise'):
for label in labels:
try:
loc = self.get_loc(label)
# get_loc returns either an integer, a slice, or a boolean
# mask
if isinstance(loc, int):
inds.append(loc)
else:
elif isinstance(loc, slice):
inds.extend(lrange(loc.start, loc.stop))
elif is_bool_indexer(loc):
if self.lexsort_depth == 0:
warnings.warn('dropping on a non-lexsorted multi-index'
'without a level parameter may impact '
'performance.',
PerformanceWarning,
stacklevel=2)
loc = loc.nonzero()[0]
inds.extend(loc)
else:
msg = 'unsupported indexer of type {}'.format(type(loc))
raise AssertionError(msg)
except KeyError:
if errors != 'ignore':
raise
Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/indexes/test_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from pandas import (date_range, MultiIndex, Index, CategoricalIndex,
compat)
from pandas.io.common import PerformanceWarning
from pandas.indexes.base import InvalidIndexError
from pandas.compat import range, lrange, u, PY3, long, lzip

Expand Down Expand Up @@ -1419,6 +1420,28 @@ def test_droplevel_multiple(self):
expected = index[:2].droplevel(2).droplevel(0)
self.assertTrue(dropped.equals(expected))

def test_drop_not_lexsorted(self):
# GH 12078

# define the lexsorted version of the multi-index
tuples = [('a', ''), ('b1', 'c1'), ('b2', 'c2')]
lexsorted_mi = MultiIndex.from_tuples(tuples, names=['b', 'c'])
self.assertTrue(lexsorted_mi.is_lexsorted())

# and the not-lexsorted version
df = pd.DataFrame(columns=['a', 'b', 'c', 'd'],
data=[[1, 'b1', 'c1', 3], [1, 'b2', 'c2', 4]])
df = df.pivot_table(index='a', columns=['b', 'c'], values='d')
df = df.reset_index()
not_lexsorted_mi = df.columns
self.assertFalse(not_lexsorted_mi.is_lexsorted())

# compare the results
self.assert_index_equal(lexsorted_mi, not_lexsorted_mi)
with self.assert_produces_warning(PerformanceWarning):
self.assert_index_equal(lexsorted_mi.drop('a'),
not_lexsorted_mi.drop('a'))

def test_insert(self):
# key contained in all levels
new_index = self.index.insert(0, ('bar', 'two'))
Expand Down