Skip to content
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

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

Closed
imre-kerr opened this issue Jun 15, 2018 · 5 comments
Closed
Labels
good first issue Indexing Related to indexing on series/frames, not to indexes themselves Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@imre-kerr
Copy link

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

@WillAyd
Copy link
Member

WillAyd commented Jun 15, 2018

That does seem strange that the uniqueness would impact the behavior here. Investigation and PRs welcome!

@WillAyd WillAyd added the Indexing Related to indexing on series/frames, not to indexes themselves label Jun 15, 2018
@toobaz
Copy link
Member

toobaz commented Jun 16, 2018

Even worse, if some keys are missing (and some are not), drop will raise on unique indexes and not on non-unique:

In [2]: pd.DataFrame(index=[1,2,3]).drop([1, 4])
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-2-e1860fd637df> in <module>()
----> 1 pd.DataFrame(index=[1,2,3]).drop([1, 4])

/home/nobackup/repo/pandas/pandas/core/frame.py in drop(self, labels, axis, index, columns, level, inplace, errors)
   3695                                            index=index, columns=columns,
   3696                                            level=level, inplace=inplace,
-> 3697                                            errors=errors)
   3698 
   3699     @rewrite_axis_style_signature('mapper', [('copy', True),

/home/nobackup/repo/pandas/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:

/home/nobackup/repo/pandas/pandas/core/generic.py in _drop_axis(self, labels, axis, level, errors)
   3138                 new_axis = axis.drop(labels, level=level, errors=errors)
   3139             else:
-> 3140                 new_axis = axis.drop(labels, errors=errors)
   3141             dropped = self.reindex(**{axis_name: new_axis})
   3142             try:

/home/nobackup/repo/pandas/pandas/core/indexes/base.py in drop(self, labels, errors)
   4345             if errors != 'ignore':
   4346                 raise KeyError(
-> 4347                     'labels %s not contained in axis' % labels[mask])
   4348             indexer = indexer[~mask]
   4349         return self.delete(indexer)

KeyError: 'labels [4] not contained in axis'

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

I think the fix is simple: this .all():

if errors == 'raise' and indexer.all():

should be .any().

@alimcmaster1
Copy link
Member

@toobaz mind if I take a look at this?

@WillAyd
Copy link
Member

WillAyd commented Jun 16, 2018

@alimcmaster1 go for it

@toobaz
Copy link
Member

toobaz commented Jun 17, 2018

@toobaz mind if I take a look at this?

@alimcmaster1 sure, as @WillAyd said. By the way, there is some scope for clearning (the set_names part is obsolete and can be removed).

@jorisvandenbossche jorisvandenbossche added the Regression Functionality that used to work in a prior pandas version label Jun 18, 2018
@jorisvandenbossche jorisvandenbossche added this to the 0.23.2 milestone Jun 18, 2018
toobaz pushed a commit that referenced this issue Jun 21, 2018
jorisvandenbossche pushed a commit that referenced this issue Jun 29, 2018
jorisvandenbossche pushed a commit that referenced this issue Jul 2, 2018
Sup3rGeo pushed a commit to Sup3rGeo/pandas that referenced this issue Oct 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Indexing Related to indexing on series/frames, not to indexes themselves Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

No branches or pull requests

5 participants