Skip to content

Unexpected behaviour while dropping #18287

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
carlosdanielcsantos opened this issue Nov 14, 2017 · 3 comments
Closed

Unexpected behaviour while dropping #18287

carlosdanielcsantos opened this issue Nov 14, 2017 · 3 comments

Comments

@carlosdanielcsantos
Copy link
Contributor

Code Sample, a copy-pastable example if possible

>>> ser = pd.Series([f for f in 'abcdefgh'])
>>> ser.drop(ser == 'c')

2    c
3    d
4    e
5    f
6    g
7    h
dtype: object

Problem description

I see what is going here, ser == 'c' results in a list of True/False (meaning 0 and 1), so it drops the 0 and 1 indices. However, this is a completely unexpected result. This should at least raise an exception. I would be happy with an error, but boolean mask support would result in a much more cleaner and simpler syntax than:

ser.drop(ser[ser == 'c'].index)

Expected Output

Raising an exception or dropping by boolean mask:

0    a
1    b
3    d
4    e
5    f
6    g
7    h
dtype: object

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None

pandas: 0.19.2
nose: None
pip: 9.0.1
setuptools: 36.6.0
Cython: None
numpy: 1.11.0
scipy: 0.18.1
statsmodels: None
xarray: None
IPython: 6.2.1
sphinx: 1.6.4
patsy: None
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: 2.1.0
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.9.6
boto: None
pandas_datareader: None

@jorisvandenbossche
Copy link
Member

This is fixed in pandas 0.21.0, and it now raises an error:

In [195]: ser = pd.Series([f for f in 'abcdefgh'])

In [196]: ser.drop(ser == 'c')
...
ValueError: labels [False False  True False False False False False] not contained in axis

(see #16877, PR #17343)

@jorisvandenbossche jorisvandenbossche added this to the No action milestone Nov 14, 2017
@carlosdanielcsantos
Copy link
Contributor Author

carlosdanielcsantos commented Nov 14, 2017

@jorisvandenbossche how about being able to drop with boolean mask? Is there any discussion going on about it?

@jorisvandenbossche
Copy link
Member

I am not sure we had such a discussion, but I personally think we should not go that way: 1) to not complicate what drop can do 2) because it is easy to do with s[~mask]. But you are welcome to open an issue for such enhancement request if you want to see the opinion of others.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants