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()
Comment From: jorisvandenbossche
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 https://github.com/pandas-dev/pandas/issues/16877, PR https://github.com/pandas-dev/pandas/pull/17343)
Comment From: carlosdanielcsantos
@jorisvandenbossche how about being able to drop with boolean mask? Is there any discussion going on about it?
Comment From: jorisvandenbossche
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.