1

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.

tgrandje
  • 2,332
  • 11
  • 33
BSalita
  • 8,420
  • 10
  • 51
  • 68

1 Answers1

2

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   │
└─────┘
ritchie46
  • 10,405
  • 1
  • 24
  • 43
  • Stringize question is answered. However, I wasn't clear that I was interested in round-tripping e.g. `eval("[(col("foo")) > (2i32)]")`. Is there something similar to eval()? – BSalita Jan 02 '23 at 14:46
  • `[(col("foo")) > (2i32)]` should be displayed as `[col("foo") > 2i32]`. The expression display logic should only add parenthesis if an inner expression contains a priority dependent operator. e.g. > but not .gt(2i32). – BSalita Jan 02 '23 at 14:50
  • 1
    For round tripping you can dump to json. Regarding the string representation. An extra bracket is not wrong, it can only be elided, you are invited to make a PR to do so. – ritchie46 Jan 02 '23 at 15:08
  • I opened an enhancement issue. Low priority of course. But if someone refactors, it could be considered. – BSalita Jan 02 '23 at 15:23
  • 1
    The more I learn polars the more I love it. This way of encoding filters is really a strong feature. – pedrosaurio Mar 15 '23 at 13:07