Skip to content

Passing empty label list to df.drop() errors when index is non-unique #21494

Closed
@imre-kerr

Description

@imre-kerr

Code Sample, a copy-pastable example if possible

# Unique index works fine
In [8]: pd.DataFrame(index=[1,2,3]).drop([])
Out[8]: 
Empty DataFrame
Columns: []
Index: [1, 2, 3]

# Non-unique index throws error
In [9]: pd.DataFrame(index=[1,1,2]).drop([])
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-9-e714e59114d2> in <module>()
----> 1 pd.DataFrame(index=[1,1,2]).drop([])

~/.virtualenvs/b5/lib/python3.6/site-packages/pandas/core/frame.py in drop(self, labels, axis, index, columns, level, inplace, errors)
   3692                                            index=index, columns=columns,
   3693                                            level=level, inplace=inplace,
-> 3694                                            errors=errors)
   3695 
   3696     @rewrite_axis_style_signature('mapper', [('copy', True),

~/.virtualenvs/b5/lib/python3.6/site-packages/pandas/core/generic.py in drop(self, labels, axis, index, columns, level, inplace, errors)
   3106         for axis, labels in axes.items():
   3107             if labels is not None:
-> 3108                 obj = obj._drop_axis(labels, axis, level=level, errors=errors)
   3109 
   3110         if inplace:

~/.virtualenvs/b5/lib/python3.6/site-packages/pandas/core/generic.py in _drop_axis(self, labels, axis, level, errors)
   3156 
   3157             if errors == 'raise' and indexer.all():
-> 3158                 raise KeyError('{} not found in axis'.format(labels))
   3159 
   3160             slicer = [slice(None)] * self.ndim

KeyError: '[] not found in axis'

Problem description

When passing a list-like to drop, it drops all the labels given. If there are no labels, it should drop nothing.

Expected Output

In [8]: pd.DataFrame(index=[1,1,2]).drop([])
Out[8]: 
Empty DataFrame
Columns: []
Index: [1, 1, 2]

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]

INSTALLED VERSIONS

commit: None
python: 3.6.3.final.0
python-bits: 64
OS: Linux
OS-release: 4.13.0-45-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.23.0
pytest: 3.5.1
pip: 10.0.1
setuptools: 39.1.0
Cython: None
numpy: 1.14.3
scipy: None
pyarrow: 0.9.0
xarray: None
IPython: 6.2.1
sphinx: None
patsy: None
dateutil: 2.7.2
pytz: 2018.4
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: 0.4.0
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    IndexingRelated to indexing on series/frames, not to indexes themselvesRegressionFunctionality that used to work in a prior pandas versiongood first issue

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions