diff --git a/.gitignore b/.gitignore index e8b557d68ac39..627ccf4112ff5 100644 --- a/.gitignore +++ b/.gitignore @@ -41,6 +41,7 @@ doc/_build dist # Egg metadata *.egg-info +.eggs # tox testing tool .tox # rope diff --git a/doc/source/whatsnew/v0.17.0.txt b/doc/source/whatsnew/v0.17.0.txt index cc044bc35a707..9c81787e19f22 100644 --- a/doc/source/whatsnew/v0.17.0.txt +++ b/doc/source/whatsnew/v0.17.0.txt @@ -65,6 +65,6 @@ Bug Fixes - Bug in ``Timestamp``'s' ``microsecond``, ``quarter``, ``dayofyear``, ``week`` and ``daysinmonth`` properties return ``np.int`` type, not built-in ``int``. (:issue:`10050`) - Bug in ``NaT`` raises ``AttributeError`` when accessing to ``daysinmonth``, ``dayofweek`` properties. (:issue:`10096`) - +- Bug in ``drop_duplicates`` dropping name(s) (:issue:`10115`) diff --git a/pandas/core/index.py b/pandas/core/index.py index 21f1fed2cd6da..5f762466ad2b4 100644 --- a/pandas/core/index.py +++ b/pandas/core/index.py @@ -2580,7 +2580,10 @@ def drop(self, labels, errors='raise'): @Appender(_shared_docs['drop_duplicates'] % _index_doc_kwargs) def drop_duplicates(self, take_last=False): result = super(Index, self).drop_duplicates(take_last=take_last) - return self._constructor(result) + if self.name is not None: + return self._constructor(result, name=self.name) + else: + return self._constructor(result, names=self.names) @Appender(_shared_docs['duplicated'] % _index_doc_kwargs) def duplicated(self, take_last=False): diff --git a/pandas/tests/test_index.py b/pandas/tests/test_index.py index 444aa2a0bab1e..b2632092ba038 100644 --- a/pandas/tests/test_index.py +++ b/pandas/tests/test_index.py @@ -4151,6 +4151,17 @@ def test_droplevel_multiple(self): expected = index[:2].droplevel(2).droplevel(0) self.assertTrue(dropped.equals(expected)) + def test_drop_duplicates_names(self): + # GH 10115 + for idx in [Index([3, 4, 5, 3]), + Index([3, 4, 5, 3], name='Num'), + MultiIndex.from_tuples([('A', 1), ('A', 2)]), + MultiIndex.from_tuples([('A', 1), ('A', 2)], names=[None, None]), + MultiIndex.from_tuples([('A', 1), ('A', 2)], names=[None, 'Num']), + MultiIndex.from_tuples([('A', 1), ('A', 2)], names=['Upper', 'Num']), + ]: + self.assertEqual(idx.drop_duplicates().names, idx.names) + def test_insert(self): # key contained in all levels new_index = self.index.insert(0, ('bar', 'two'))