Repository: pandas-dev/pandas

Issue:
Calling drop_duplicates method for empty pandas dataframe throws error
#### Code Sample

```python
>>> pd.DataFrame().drop_duplicates()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/analytical-monk/miniconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 3098, in drop_duplicates
    duplicated = self.duplicated(subset, keep=keep)
  File "/home/analytical-monk/miniconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 3144, in duplicated
    labels, shape = map(list, zip(*map(f, vals)))
ValueError: not enough values to unpack (expected 2, got 0)
```
#### Problem description

Currently, calling the drop_duplicates method for an empty dataframe object (simply pd.DataFrame()) throws an error.
Ideally it should return back the empty dataframe just liked it does when at least one column is present.

#### Expected Output

```
>>> pd.DataFrame().drop_duplicates()

Empty DataFrame
Columns: []
Index: []
```

#### Output of ``pd.show_versions()``

<details>

```
>>> pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 3.6.1.final.0
python-bits: 64
OS: Linux
OS-release: 4.8.0-58-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_IN
LOCALE: en_IN.ISO8859-1

pandas: 0.20.3
pytest: None
pip: 9.0.1
setuptools: 36.6.0
Cython: None
numpy: 1.13.1
scipy: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: 4.6.0
html5lib: 1.0b10
sqlalchemy: 1.1.14
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
pandas_gbq: None
pandas_datareader: None
```
</details>



Discussion:
@TomAugspurger Opened this issue for the problem I'd mentioned in the gitter chat.
Can I work on this issue? @TomAugspurger 
Please do!

On Wed, Mar 28, 2018 at 1:35 PM, Arpit Solanki <notifications@github.com>
wrote:

> Can I work on this issue? @TomAugspurger
> <https://github.com/TomAugspurger>
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <https://github.com/pandas-dev/pandas/issues/20516#issuecomment-376991195>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/ABQHIlMYlNyhe2x7TrsfjIMGzOm9Y4KJks5ti9gKgaJpZM4S-lLY>
> .
>

@arpit1997 are you still working on this?

Resolving patch:
diff --git a/pandas/core/frame.py b/pandas/core/frame.py
--- a/pandas/core/frame.py
+++ b/pandas/core/frame.py
@@ -4335,6 +4335,9 @@ def drop_duplicates(self, subset=None, keep='first', inplace=False):
         -------
         deduplicated : DataFrame
         """
+        if self.empty:
+            return self.copy()
+
         inplace = validate_bool_kwarg(inplace, 'inplace')
         duplicated = self.duplicated(subset, keep=keep)
 
@@ -4369,6 +4372,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))


Test patch:
diff --git a/pandas/tests/frame/test_duplicates.py b/pandas/tests/frame/test_duplicates.py
--- a/pandas/tests/frame/test_duplicates.py
+++ b/pandas/tests/frame/test_duplicates.py
@@ -263,6 +263,23 @@ def test_drop_duplicates_tuple():
     tm.assert_frame_equal(result, expected)
 
 
+@pytest.mark.parametrize('df', [
+    DataFrame(),
+    DataFrame(columns=[]),
+    DataFrame(columns=['A', 'B', 'C']),
+    DataFrame(index=[]),
+    DataFrame(index=['A', 'B', 'C'])
+])
+def test_drop_duplicates_empty(df):
+    # GH 20516
+    result = df.drop_duplicates()
+    tm.assert_frame_equal(result, df)
+
+    result = df.copy()
+    result.drop_duplicates(inplace=True)
+    tm.assert_frame_equal(result, df)
+
+
 def test_drop_duplicates_NA():
     # none
     df = DataFrame({'A': [None, None, 'foo', 'bar',