diff --git a/pandas/core/frame.py b/pandas/core/frame.py index ffb2ad046158f..a0559cef6831d 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4354,6 +4354,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.columns.empty: + return Series() + def f(vals): labels, shape = algorithms.factorize( vals, size_hint=min(len(self), _SIZE_HINT_LIMIT)) diff --git a/pandas/tests/frame/test_analytics.py b/pandas/tests/frame/test_analytics.py index d1a4a5f615b86..66a5c181b1369 100644 --- a/pandas/tests/frame/test_analytics.py +++ b/pandas/tests/frame/test_analytics.py @@ -1522,6 +1522,16 @@ def test_drop_duplicates_with_duplicate_column_names(self): expected1 = df[:2] tm.assert_frame_equal(result1, expected1) + def test_drop_duplicates_with_no_column(self): + # GH20516 + df0 = DataFrame() + result0 = df0.drop_duplicates() + tm.assert_frame_equal(result0, df0) + + df1 = DataFrame(index=[1, 2]) + result1 = df1.drop_duplicates() + tm.assert_frame_equal(result1, df1) + def test_drop_duplicates_for_take_all(self): df = DataFrame({'AAA': ['foo', 'bar', 'baz', 'bar', 'foo', 'bar', 'qux', 'foo'],