I want to apply filtering to Polars Dataframe, but each element of the filter is optional, dependent on the availablity of a function parameter.
I have a function that takes the following 3 paramters: ticker: str = '', strategy: str = '', iteration: int = -1
If these parameters are not the default value then I want to apply that filter to the dataframe. My logic is as follows:
self.configs_df = self.configs_df.filter(
(ticker != '' & pl.col('Ticker') == ticker) &
(strategy != '' & pl.col('Strategy') == strategy) &
(iteration >= 0 & pl.col('Iteration') == iteration)
)
but I get an error: TypeError: the truth value of an Expr is ambiguous which I understand, but I don't know how to build the expression up in its logical parts. Any help or guidance would be much appreciated as I am an absolute beginner with Polars, having come from Pandas, where I have this (similar) logic working.
Regards, Stuart
ifto run code with one filter or skip it. Eventually... if (ticker != '') else ...to getpl.col('Ticker') == tickeror to get other structure which would select all rows. But I'm not experience with Polars