Home > other >  Possible to Stringize a Polars Expression?
Possible to Stringize a Polars Expression?

Time:01-03

Is it possible to stringize a Polars expression and vice-versa(? For example, convert df.filter(pl.col('a')<10) to a string of "df.filter(pl.col('a')<10)". Is roundtripping possible e.g. eval("df.filter(pl.col('a')<10)") for user input or tool automation. I know this can be done with a SQL expression but I'm interested in native. I want to show the specified filter in the title of plots.

CodePudding user response:

Expressions

>>> expr = pl.col("foo") > 2
>>> print(str(expr))
[(col("foo")) > (2i32)]

LazyFrames

>>> df = pl.DataFrame({
...     "foo": [1, 2, 3]
... })
>>> json_state = df.lazy().filter(expr).write_json()
>>> query_plan = pl.LazyFrame.from_json(json_state)
>>> query_plan.collect()
shape: (1, 1)
┌─────┐
│ foo │
│ --- │
│ i64 │
╞═════╡
│ 3   │
└─────┘
  • Related