Skip to content

Commit 47a8111

Browse files
lkirkTomAugspurger
authored andcommitted
Error with .drop([]) on non-unique index (#16428)
(cherry picked from commit b0a51df)
1 parent 2a008a9 commit 47a8111

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

doc/source/whatsnew/v0.20.2.txt

+2
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,5 @@ Categorical
100100

101101
Other
102102
^^^^^
103+
104+
- Bug in ``pd.drop([])`` for DataFrame with non-unique indices (:issue:`16270`)

pandas/core/generic.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from pandas._libs import tslib, lib
1313
from pandas.core.dtypes.common import (
1414
_ensure_int64,
15+
_ensure_object,
1516
needs_i8_conversion,
1617
is_scalar,
1718
is_number,
@@ -2062,7 +2063,7 @@ def drop(self, labels, axis=0, level=None, inplace=False, errors='raise'):
20622063
result = dropped
20632064

20642065
else:
2065-
labels = com._index_labels_to_array(labels)
2066+
labels = _ensure_object(com._index_labels_to_array(labels))
20662067
if level is not None:
20672068
if not isinstance(axis, MultiIndex):
20682069
raise AssertionError('axis must be a MultiIndex')

pandas/tests/frame/test_axis_select_reindex.py

+6
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ def test_drop_names(self):
6161
expected = Index(['e', 'f'], name='second')
6262
tm.assert_index_equal(dropped.columns, expected)
6363

64+
# GH 16398
65+
dropped = df.drop([], errors='ignore')
66+
expected = Index(['a', 'b', 'c'], name='first')
67+
tm.assert_index_equal(dropped.index, expected)
68+
6469
def test_drop_col_still_multiindex(self):
6570
arrays = [['a', 'b', 'c', 'top'],
6671
['', '', '', 'OD'],
@@ -100,6 +105,7 @@ def test_drop(self):
100105
columns=['a', 'a', 'b'])
101106
assert_frame_equal(nu_df.drop('a', axis=1), nu_df[['b']])
102107
assert_frame_equal(nu_df.drop('b', axis='columns'), nu_df['a'])
108+
assert_frame_equal(nu_df.drop([]), nu_df) # GH 16398
103109

104110
nu_df = nu_df.set_index(pd.Index(['X', 'Y', 'X']))
105111
nu_df.columns = list('abc')

0 commit comments

Comments
 (0)