polars.sql_expr#

polars.sql_expr(sql: str) Expr[source]#
polars.sql_expr(sql: Sequence[str]) list[Expr]

Parse one or more SQL expressions to polars expression(s).

Parameters:
sql

One or more SQL expressions.

Examples

Parse a single SQL expression:

>>> df = pl.DataFrame({"a": [2, 1]})
>>> expr = pl.sql_expr("MAX(a)")
>>> df.select(expr)
shape: (1, 1)
┌─────┐
│ a   │
│ --- │
│ i64 │
╞═════╡
│ 2   │
└─────┘

Parse multiple SQL expressions:

>>> df.with_columns(
...     *pl.sql_expr(["POWER(a,a) AS a_a", "CAST(a AS TEXT) AS a_txt"]),
... )
shape: (2, 3)
┌─────┬─────┬───────┐
│ a   ┆ a_a ┆ a_txt │
│ --- ┆ --- ┆ ---   │
│ i64 ┆ f64 ┆ str   │
╞═════╪═════╪═══════╡
│ 2   ┆ 4.0 ┆ 2     │
│ 1   ┆ 1.0 ┆ 1     │
└─────┴─────┴───────┘