surfacepipelinepython
Pipelinepipeline · returns table

PYTHON

Transform query results with inline Python

Table-in, table-out — composes downstream of SELECTs.

pipelinellmpipeline-composabletext

Syntax

THEN PYTHON {{ code }}
THEN PYTHON({{ code }})

Arguments

nametypedescription
codeVARCHAR
_tableTABLE

About

PIPELINE cascade for inline Python table transforms. Executes user-provided Python against the full pipeline input table. This is an escape hatch for transformations that are awkward in SQL. Usage: SELECT * FROM sales THEN PYTHON('result = df.sort_values("revenue", ascending=False)') SELECT * FROM sales THEN PYTHON(''' df["margin_pct"] = (df["profit"] / df["revenue"]) * 100 result = df[df["margin_pct"] > 20] ''')

Examples

Executes inline Python transforms against the pipeline dataframe

SELECT
  *
FROM
  (
    VALUES
      (1),
      (2)
  ) AS t (x) THEN PYTHON ('df["double"] = df["x"] * 2; result = df') THEN PYTHON (
    'result = pd.DataFrame({"double_first":[int(df.iloc[0]["double"])]})'
  )

Nearby rabbit holes

same domain
Climb back to The Looking Glass