Skip to content

ERR: could pandas suggest to use bitwise operators to filter instead of and and or, as on Kaggle? #35037

@MarcoGorelli

Description

@MarcoGorelli

Currently, if one writes

>>> import pandas as pd
>>> df = pd.DataFrame([1,2,3])
>>> df
   0
0  1
1  2
2  3
>>> df[df[0].ge(0) and df[0].lt(0)]

then the traceback shows

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/marco/pandas-dev/pandas/core/generic.py", line 1326, in __nonzero__
    raise ValueError(
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I've noticed that Kaggle add their own warning when someone tries to do this:

To combine filtering conditions in Pandas, use bitwise operators ('&' and '|') not pure Python ones ('and' and 'or')

See below:

image


Is this something that could be in pandas as well? Sure, there's no way of reading users' minds - perhaps they reached this error by some other means - but would the error message be clearer if there was an extra line reading

If you were trying to combine filtering conditions in Pandas, use bitwise operators ('&' and '|') instead of pure Python ones ('and' and 'or')

?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions