An expression language
for Polars

polars-expr-transformer turns formulas like if [age] > 30 then "Senior" else "Junior" endif into Polars expressions. It is also the expression language used in Flowfile. The playground below runs in your browser, nothing to install.

pip install polars-expr-transformer

Playground

Pick a dataset and type an expression. The result and the generated Polars and FlowFrame code update as you type. Everything runs in your browser. Not sure of the syntax? Describe what you want in plain English and generate a flowfile formula.

Loading Python runtime…
Input data
Expression
Ctrl/⌘ + Enter to run
Results will appear here once the runtime is ready.

Syntax

The syntax is close to what you would write in a spreadsheet formula or a SQL CASE statement.

Column references

Wrap column names in square brackets. Spaces are fine.

[salary]
[Order Date]
[first_name]

Literals

Strings use single or double quotes; integers, floats and booleans are bare. null is the missing value.

"hello"   'world'
42   3.14   -7
true   false   null

Conditionals

if … then … else … endif, with optional elseif branches. Becomes pl.when().then().otherwise().

if [score] >= 90 then "A"
elseif [score] >= 80 then "B"
else "C" endif

Functions

There are 95 built-in functions, listed in the reference below. Calls can be nested.

uppercase(left([last_name], 3))
round([price] * [qty], 2)
coalesce([nick], [name], "n/a")

Comments

Everything after // on a line is ignored.

[price] * [quantity] // subtotal
  - ifnull([discount], 0) // minus discount

Operators

+ - * / %arithmetic (and string + concat)
= == !=equality
> >= < <=comparison
and orboolean logic
( )grouping

Function reference

This reference is generated from the library's docstrings. Unless noted, every argument accepts a literal value, a [column] reference or a nested expression. Click Try it ▸ to load an example into the playground.

How it works

1

Parse

The expression is tokenized and each token is classified: columns, literals, operators, functions and if/then keywords.

2

Build

The tokens are arranged into a function tree, applying operator precedence.

3

Convert

The tree is turned into a pl.Expr, or into source code via to_polars_code() and to_flowframe_code(). Execution is handled by Polars itself.

Use it in Python

import polars as pl
from polars_expr_transformer import simple_function_to_expr, to_polars_code

df = pl.DataFrame({"name": ["Alice", "Bob"], "age": [30, 25]})

# Run an expression directly
df = df.with_columns(
    simple_function_to_expr('if [age] >= 30 then "Senior" else "Junior" endif').alias("level")
)

# …or generate the equivalent Polars source code
print(to_polars_code('uppercase([name])'))
# pl.col("name").str.to_uppercase()

About this page: the playground loads Pyodide, the Polars WebAssembly build and the polars-expr-transformer wheel built from this repository. Expressions are evaluated by the library itself, in the browser.