Skip to content

Commit 03af0c1

Browse files
committed
Bug in :meth:DataFrame.drop_duplicatesfor empty DataFrame throws error (:issue:20516)
1 parent b5d81cf commit 03af0c1

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

doc/source/whatsnew/v0.24.0.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ Reshaping
711711
- Bug in :func:`get_dummies` with Unicode attributes in Python 2 (:issue:`22084`)
712712
- Bug in :meth:`DataFrame.replace` raises ``RecursionError`` when replacing empty lists (:issue:`22083`)
713713
- 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`)
714-
-
714+
- Bug in :meth:`DataFrame.drop_duplicates`for empty DataFrame throws error (:issue:`20516`)
715715

716716
Build Changes
717717
^^^^^^^^^^^^^

pandas/core/frame.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4369,6 +4369,9 @@ def duplicated(self, subset=None, keep='first'):
43694369
from pandas.core.sorting import get_group_index
43704370
from pandas._libs.hashtable import duplicated_int64, _SIZE_HINT_LIMIT
43714371

4372+
if self.empty:
4373+
return Series()
4374+
43724375
def f(vals):
43734376
labels, shape = algorithms.factorize(
43744377
vals, size_hint=min(len(self), _SIZE_HINT_LIMIT))

pandas/tests/frame/test_duplicates.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,13 @@ def test_drop_duplicates_tuple():
263263
tm.assert_frame_equal(result, expected)
264264

265265

266+
def test_drop_duplicates_empty():
267+
# GH 20516
268+
df = DataFrame()
269+
result = df.drop_duplicates()
270+
tm.assert_frame_equal(result, df)
271+
272+
266273
def test_drop_duplicates_NA():
267274
# none
268275
df = DataFrame({'A': [None, None, 'foo', 'bar',

0 commit comments

Comments
 (0)