I would like to achieve the following via Polars expressions, as opposed to mapping the elements row-by-row, but I have not been able to figure out a way.
import polars
def foo():
return 1 + 1
def bar():
return 1 + 1
def baz():
return 2 + 2
exprs = 1_000_000 * [
"foo() == bar()",
"bar() == baz()",
"foo() == baz()",
"foo() > bar()",
"bar() < baz()",
"foo() != baz()",
]
df = polars.DataFrame({"exprs": exprs})
df.with_columns(
polars.col("exprs").map_elements(
lambda x: eval(x), return_dtype=polars.datatypes.Boolean
)
)
shape: (6_000_000, 1)
┌───────┐
│ exprs │
│ --- │
│ bool │
╞═══════╡
│ true │
│ false │
│ false │
│ false │
│ true │
│ … │
│ false │
│ false │
│ false │
│ true │
│ true │
└───────┘
The issue being that foo()
and bar()
are non-trivial and so map_elements()
takes some time to complete since the DataFrame
is large.
5