Skip to content

BUG: drop_duplicates drops name(s). #10116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ doc/_build
dist
# Egg metadata
*.egg-info
.eggs
# tox testing tool
.tox
# rope
Expand Down
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.17.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`)


5 changes: 4 additions & 1 deletion pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is for dealing with the API differences between Index and MultiIndex, yes? I think it would be cleaner to override the method on MultiIndex.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. OK, I can do that.

Alternatively, it would be great if the constructors of all Index subclasses (other than MultiIndex) accepted a names list of length one and used it to set name. That way one could always just set names without worrying about it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would even be happy if you just explicitly check isinstance(self, MultiIndex).

I agree that your alternative solution would be preferable. That's part of a much bigger issue: #3268

return self._constructor(result, name=self.name)
else:
return self._constructor(result, names=self.names)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can dispense with this with a

return self._shallow_copy(result), should handle all meta-data

e.g. this will propogate things like freq/tz, etc as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

though looking at this now, I think MultiIndex._shallow_copy is wrong

let me see if I can fix this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


@Appender(_shared_docs['duplicated'] % _index_doc_kwargs)
def duplicated(self, take_last=False):
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down