Skip to content

BUG: drop(level=.) doesn't raise if label is absent #18561

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

Closed
toobaz opened this issue Nov 29, 2017 · 3 comments · Fixed by #41712
Closed

BUG: drop(level=.) doesn't raise if label is absent #18561

toobaz opened this issue Nov 29, 2017 · 3 comments · Fixed by #41712
Labels
Error Reporting Incorrect or improved errors from pandas good first issue MultiIndex Needs Tests Unit test(s) needed to prevent regressions Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@toobaz
Copy link
Member

toobaz commented Nov 29, 2017

Code Sample, a copy-pastable example if possible

pd.DataFrame(index=pd.MultiIndex.from_product([range(3), range(3)])).drop(5) # Raises
pd.DataFrame(index=pd.MultiIndex.from_product([range(3), range(3)])).drop(5, level=0) # Same operation, does nothing, but silently

Problem description

Since one expects drop() to always raise an error if the requested label is not found, this can be misleading.

It is not even a matter of partial vs. full labels, since the first example above has a partial label.

Expected Output

The same.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: 2a0e54b
python: 3.5.3.final.0
python-bits: 64
OS: Linux
OS-release: 4.9.0-4-amd64
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: it_IT.UTF-8
LOCALE: it_IT.UTF-8

pandas: 0.22.0.dev0+251.g2a0e54bc8.dirty
pytest: 3.2.3
pip: 9.0.1
setuptools: 36.7.0
Cython: 0.25.2
numpy: 1.12.1
scipy: 0.19.0
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: 1.5.6
patsy: 0.4.1
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: 1.2.0dev
tables: 3.3.0
numexpr: 2.6.1
feather: 0.3.1
matplotlib: 2.0.0
openpyxl: None
xlrd: 1.0.0
xlwt: 1.1.2
xlsxwriter: 0.9.6
lxml: None
bs4: 4.5.3
html5lib: 0.999999999
sqlalchemy: 1.0.15
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: 0.2.1

@jorisvandenbossche
Copy link
Member

Not sure if related, but also one with duplicate values does not raise:

In [23]: pd.DataFrame(index=[1, 1, 2]).drop(5) # Raises
Out[23]: 
Empty DataFrame
Columns: []
Index: [1, 1, 2]

In [24]: pd.DataFrame(index=[1, 2]).drop(5) # Raises
...
ValueError: labels [5] not contained in axis

@jorisvandenbossche
Copy link
Member

Also unique MultiIndex fails to raise, so it are two different bugs:

In [27]: pd.MultiIndex.from_tuples([(1, 2), (3, 4)]).drop(5, level=0)
Out[27]: 
MultiIndex(levels=[[1, 3], [2, 4]],
           labels=[[0, 1], [0, 1]])

For this one, MultiIndex._drop_from_level is called, and in that code branch, errors is just not passed or implemented.

@jreback jreback added Difficulty Intermediate Error Reporting Incorrect or improved errors from pandas MultiIndex Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Nov 29, 2017
@jreback jreback added this to the Next Major Release milestone Nov 29, 2017
@jorisvandenbossche jorisvandenbossche changed the title drop(level=.) doesn't raise if label is absent BUG: drop(level=.) doesn't raise if label is absent Jan 11, 2018
@phofl
Copy link
Member

phofl commented Nov 12, 2020

This works now. Maybe needs tests

@phofl phofl added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Bug labels Nov 12, 2020
@mroeschke mroeschke mentioned this issue May 29, 2021
15 tasks
@mroeschke mroeschke modified the milestones: Contributions Welcome, 1.3 May 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Error Reporting Incorrect or improved errors from pandas good first issue MultiIndex Needs Tests Unit test(s) needed to prevent regressions Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants