machow / siuba

Python library for using dplyr like syntax with pandas and SQL
https://siuba.org
MIT License
1.15k stars 49 forks source link

More complex use of filter #470

Closed HakonSkogsrud closed 1 year ago

HakonSkogsrud commented 1 year ago

Hi!

Thanks for creating a brilliant package, I use it a lot and love it!

I struggle to make filter work with more complex examples. See this example using pandas how I want to filter:

from siuba import _, filter
from siuba.data import mtcars

mtcars.query("\
                (cyl ==4 and am == 0 ) \
                or \
                (gear == 5) \
            ")

if I try using filter I get an error message:

mtcars >> filter((_.cyl == 4 & _.am==0) | (_.gear == 5))

TypeError: Symbolic objects can not be converted to True/False, or used with these keywords: not, and, or.

Is there a way to achieve this using filter?

Thanks a lot!

machow commented 1 year ago

Hey! Similar to using with or (|) you need to put the left hand and right hand sides of the & in parentheses.

It's unfortunately a weird issue with python syntax :/

https://siuba.org/guide/verb-filter.html#filters-with-or-conditions

HakonSkogsrud commented 1 year ago

🤦‍♂️ I did not realize I needed to encapsulate all expressions in parentheses.

Thanks a lot for your reply and have nice day!