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..a5d4a1609647e 100644 --- a/pandas/core/index.py +++ b/pandas/core/index.py @@ -2579,14 +2579,12 @@ 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) + return super(Index, self).drop_duplicates(take_last=take_last) @Appender(_shared_docs['duplicated'] % _index_doc_kwargs) def duplicated(self, take_last=False): return super(Index, self).duplicated(take_last=take_last) - def _evaluate_with_timedelta_like(self, other, op, opstr): raise TypeError("can only perform ops with timedelta like values") diff --git a/pandas/tests/test_index.py b/pandas/tests/test_index.py index 444aa2a0bab1e..9cff4c432592f 100644 --- a/pandas/tests/test_index.py +++ b/pandas/tests/test_index.py @@ -199,10 +199,19 @@ def test_duplicates(self): if not len(ind): continue + if isinstance(ind, MultiIndex): + continue idx = self._holder([ind[0]]*5) self.assertFalse(idx.is_unique) self.assertTrue(idx.has_duplicates) + # GH 10115 + # preserve names + idx.name = 'foo' + result = idx.drop_duplicates() + self.assertEqual(result.name, 'foo') + self.assert_index_equal(result, Index([ind[0]],name='foo')) + def test_sort(self): for ind in self.indices.values(): self.assertRaises(TypeError, ind.sort) @@ -1695,9 +1704,10 @@ def test_reindexing(self): def test_duplicates(self): - idx = CategoricalIndex([0, 0, 0]) + idx = CategoricalIndex([0, 0, 0],name='foo') self.assertFalse(idx.is_unique) self.assertTrue(idx.has_duplicates) + self.assertEqual(idx.name,'foo') def test_get_indexer(self): @@ -4410,6 +4420,19 @@ def check(nlevels, with_nulls): self.assert_array_equal(mi.duplicated(), np.zeros(len(mi), dtype='bool')) + def test_duplicate_meta_data(self): + # GH 10115 + index = MultiIndex(levels=[[0, 1], [0, 1, 2]], + labels=[[0, 0, 0, 0, 1, 1, 1], + [0, 1, 2, 0, 0, 1, 2]]) + for idx in [index, + index.set_names([None, None]), + index.set_names([None, 'Num']), + index.set_names(['Upper','Num']), + ]: + self.assertTrue(idx.has_duplicates) + self.assertEqual(idx.drop_duplicates().names, idx.names) + def test_tolist(self): result = self.index.tolist() exp = list(self.index.values)