Skip to content

Commit 9f68a96

Browse files
jonaslbjreback
authored andcommitted
BUG: DataFrame.drop() does nothing for non-unique MultiIndex
closes #12701 closes #12783
1 parent a6b1a22 commit 9f68a96

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

doc/source/whatsnew/v0.18.1.txt

+3
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ Bug Fixes
188188
- Bug in ``GroupBy.filter`` when ``dropna=False`` and no groups fulfilled the criteria (:issue:`12768`)
189189

190190

191+
- Bug in ``.drop()`` with a non-unique ``MultiIndex``. (:issue:`12701`)
192+
193+
191194

192195
- Bug in ``Timestamp.__repr__`` that caused ``pprint`` to fail in nested structures (:issue:`12622`)
193196
- Bug in ``Timedelta.min`` and ``Timedelta.max``, the properties now report the true minimum/maximum ``timedeltas`` as recognized by Pandas. See :ref:`documentation <timedeltas.limitations>`. (:issue:`12727`)

pandas/core/generic.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1881,8 +1881,7 @@ def drop(self, labels, axis=0, level=None, inplace=False, errors='raise'):
18811881
if level is not None:
18821882
if not isinstance(axis, MultiIndex):
18831883
raise AssertionError('axis must be a MultiIndex')
1884-
indexer = ~lib.ismember(
1885-
axis.get_level_values(level).values, set(labels))
1884+
indexer = ~axis.get_level_values(level).isin(labels)
18861885
else:
18871886
indexer = ~axis.isin(labels)
18881887

pandas/tests/test_multilevel.py

+19
Original file line numberDiff line numberDiff line change
@@ -1948,6 +1948,25 @@ def test_drop_level(self):
19481948
expected = self.frame.ix[[0, 2, 3, 6, 7, 9]].T
19491949
assert_frame_equal(result, expected)
19501950

1951+
def test_drop_level_nonunique_datetime(self):
1952+
# GH 12701
1953+
idx = pd.Index([2, 3, 4, 4, 5], name='id')
1954+
idxdt = pd.to_datetime(['201603231400',
1955+
'201603231500',
1956+
'201603231600',
1957+
'201603231600',
1958+
'201603231700'])
1959+
df = DataFrame(np.arange(10).reshape(5, 2),
1960+
columns=list('ab'), index=idx)
1961+
df['tstamp'] = idxdt
1962+
df = df.set_index('tstamp', append=True)
1963+
ts = pd.Timestamp('201603231600')
1964+
self.assertFalse(df.index.is_unique)
1965+
1966+
result = df.drop(ts, level='tstamp')
1967+
expected = df.loc[idx != 4]
1968+
assert_frame_equal(result, expected)
1969+
19511970
def test_drop_preserve_names(self):
19521971
index = MultiIndex.from_arrays([[0, 0, 0, 1, 1, 1],
19531972
[1, 2, 3, 1, 2, 3]],

0 commit comments

Comments
 (0)