-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Passing empty label list to df.drop() errors when index is non-unique #21494
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
Comments
That does seem strange that the uniqueness would impact the behavior here. Investigation and PRs welcome! |
Even worse, if some keys are missing (and some are not), 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 Line 3157 in 9d61ab5
should be |
@toobaz mind if I take a look at this? |
@alimcmaster1 go for it |
@alimcmaster1 sure, as @WillAyd said. By the way, there is some scope for clearning (the |
Code Sample, a copy-pastable example if possible
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
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
The text was updated successfully, but these errors were encountered: