Skip to content
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

BUG: drop_duplicates not raising KeyError on missing key #19730

Merged
merged 5 commits into from
Feb 21, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,8 @@ Indexing
- Bug in :class:`IntervalIndex` where empty and purely NA data was constructed inconsistently depending on the construction method (:issue:`18421`)
- Bug in :func:`IntervalIndex.symmetric_difference` where the symmetric difference with a non-``IntervalIndex`` did not raise (:issue:`18475`)
- Bug in :class:`IntervalIndex` where set operations that returned an empty ``IntervalIndex`` had the wrong dtype (:issue:`19101`)
- Bug in :meth:`DataFrame.drop_duplicates` where no ``KeyError`` is raised when passing in columns that don't exist on the ``DataFrame`` (issue:`19726`)


MultiIndex
^^^^^^^^^^
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3655,6 +3655,10 @@ def f(vals):
isinstance(subset, tuple) and subset in self.columns):
subset = subset,

for name in subset:
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a comment here on what you are checking

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 do this

diff = pd.Index(subset).difference(self.columns)
if len(diff):
     raise KeyError(diff)

if name not in self.columns:
raise KeyError(name)

vals = (col.values for name, col in self.iteritems()
if name in subset)
labels, shape = map(list, zip(*map(f, vals)))
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/frame/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1492,6 +1492,15 @@ def test_drop_duplicates(self):
for keep in ['first', 'last', False]:
assert df.duplicated(keep=keep).sum() == 0

def test_drop_duplicates_with_misspelled_column_name(self):
# GH 18XXX
df = pd.DataFrame({'A': [0, 1, 2, 3, 4, 5],
'B': [0, 1, 2, 3, 4, 5],
'C': [0, 1, 2, 3, 4, 6]})

with pytest.raises(KeyError):
df.drop_duplicates(['a', 'B'])

def test_drop_duplicates_with_duplicate_column_names(self):
# GH17836
df = DataFrame([
Expand Down