diff --git a/doc/source/whatsnew/v0.20.2.txt b/doc/source/whatsnew/v0.20.2.txt index e0857019d2fd4..7231b497c4bd4 100644 --- a/doc/source/whatsnew/v0.20.2.txt +++ b/doc/source/whatsnew/v0.20.2.txt @@ -93,3 +93,5 @@ Categorical Other ^^^^^ + +- Bug in ``pd.drop([])`` for DataFrame with non-unique indices (:issue:`16270`) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 2e7d8693d48dd..b19a0751ea2e4 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -12,6 +12,7 @@ from pandas._libs import tslib, lib from pandas.core.dtypes.common import ( _ensure_int64, + _ensure_object, needs_i8_conversion, is_scalar, is_number, @@ -2076,7 +2077,7 @@ def drop(self, labels, axis=0, level=None, inplace=False, errors='raise'): result = dropped else: - labels = com._index_labels_to_array(labels) + labels = _ensure_object(com._index_labels_to_array(labels)) if level is not None: if not isinstance(axis, MultiIndex): raise AssertionError('axis must be a MultiIndex') diff --git a/pandas/tests/frame/test_axis_select_reindex.py b/pandas/tests/frame/test_axis_select_reindex.py index a6326083c1bee..87d942101f5f1 100644 --- a/pandas/tests/frame/test_axis_select_reindex.py +++ b/pandas/tests/frame/test_axis_select_reindex.py @@ -61,6 +61,11 @@ def test_drop_names(self): expected = Index(['e', 'f'], name='second') tm.assert_index_equal(dropped.columns, expected) + # GH 16398 + dropped = df.drop([], errors='ignore') + expected = Index(['a', 'b', 'c'], name='first') + tm.assert_index_equal(dropped.index, expected) + def test_drop_col_still_multiindex(self): arrays = [['a', 'b', 'c', 'top'], ['', '', '', 'OD'], @@ -100,6 +105,7 @@ def test_drop(self): columns=['a', 'a', 'b']) assert_frame_equal(nu_df.drop('a', axis=1), nu_df[['b']]) assert_frame_equal(nu_df.drop('b', axis='columns'), nu_df['a']) + assert_frame_equal(nu_df.drop([]), nu_df) # GH 16398 nu_df = nu_df.set_index(pd.Index(['X', 'Y', 'X'])) nu_df.columns = list('abc')