Skip to content

Bug in DataFrame.drop_duplicates for empty DataFrame throws error #22394

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

Merged
merged 14 commits into from
Aug 23, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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: 1 addition & 1 deletion doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ Reshaping
- Bug in :func:`get_dummies` with Unicode attributes in Python 2 (:issue:`22084`)
- Bug in :meth:`DataFrame.replace` raises ``RecursionError`` when replacing empty lists (:issue:`22083`)
- Bug in :meth:`Series.replace` and meth:`DataFrame.replace` when dict is used as the `to_replace` value and one key in the dict is is another key's value, the results were inconsistent between using integer key and using string key (:issue:`20656`)
-
- Bug in :meth:`DataFrame.drop_duplicates` for empty ``DataFrame`` which incorrectly raises error (:issue:`20516`)
Copy link
Contributor

Choose a reason for hiding this comment

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

raises an error


Build Changes
^^^^^^^^^^^^^
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4369,6 +4369,9 @@ def duplicated(self, subset=None, keep='first'):
from pandas.core.sorting import get_group_index
from pandas._libs.hashtable import duplicated_int64, _SIZE_HINT_LIMIT

if self.empty:
return Series()

def f(vals):
labels, shape = algorithms.factorize(
vals, size_hint=min(len(self), _SIZE_HINT_LIMIT))
Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/frame/test_duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,30 @@ def test_drop_duplicates_tuple():
tm.assert_frame_equal(result, expected)


def test_drop_duplicates_empty():
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 parameterize this on columns with [], ['A', 'B','C'] as the values
IOW test the DataFrame(columns=[]) and DataFrame(columns=['A', 'B', 'C'])

Copy link
Contributor

Choose a reason for hiding this comment

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

actually also need a test with no columns but an index e.g. DataFrame(index=[1, 2])

so don't need to parameterize but test all of these cases (where the result is the input)

# GH 20516
expected = DataFrame()
result = expected.drop_duplicates()
tm.assert_frame_equal(result, expected)

expected = DataFrame(columns=[])
result = expected.drop_duplicates()
tm.assert_frame_equal(result, expected)

df = DataFrame(columns=['A', 'B', 'C'])
result = df.drop_duplicates()
expected = DataFrame(columns=[]) # The column infos are not carrying over
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't find this comment useful, rather put a comment about testing with empty columns, and below about an empty index

tm.assert_frame_equal(result, expected)

expected = DataFrame(index=[])
result = expected.drop_duplicates()
tm.assert_frame_equal(result, expected)

expected = DataFrame(index=['A', 'B', 'C'])
result = expected.drop_duplicates()
tm.assert_frame_equal(result, expected)


def test_drop_duplicates_NA():
# none
df = DataFrame({'A': [None, None, 'foo', 'bar',
Expand Down