Skip to content

Export a recipe to SQL, pandas and dplyr

Take your cleaning out of the app as code, and choose Replay or Refit for how learned values behave.

Applied ~8 min7 steps

You will be able to

  • Export a recipe to SQL, pandas or dplyr
  • Tell Replay from Refit
  • Know when to use scikit-learn or tidymodels recipes instead

Read first

01

A recipe is code you haven't looked at yet

Read

Every recipe compiles to code. Open a recipe in Prepare (⌘3) and use Code in its toolbar.

TargetWhat you get
SQLThe DuckDB SQL the app runs
pandasPython with pandas, same output
dplyrR with dplyr, same output
scikit-learnA Pipeline to fit on training data
recipesA tidymodels recipe to prep on training data

Copy takes the text, and Open as script creates a script in your project.

02

Replay or Refit?

Predict

Predict first

A step fills missing income with a learned median. You want the exported code to reproduce the app's output exactly. Which mode do you choose?

ReplayRefit
Reveal

The answer: Replay

Replay writes the learned values into the code, so it reproduces the app. Refit recomputes them from whatever data the code runs on, which is what you want for next month's file. Neither is more correct; they answer different needs.

03

See the difference

In the app

Open Code on a recipe that has a Fill missing values step, and choose pandas. Toggle between Replay and Refit and find the median in the code.

Under Replay you see a literal number. Under Refit you see a calculation.

The scikit-learn and recipes targets are always Refit, because fitting on training data is their whole purpose.

04

What the two dialects look like

Read

The same step, in the two forms a Refit export might take:

Python
import pandas as pd

df = pd.read_csv("customers.csv")
df["income"] = df["income"].fillna(df["income"].median())
R
library(dplyr)

df <- read.csv("customers.csv")
df <- df |> mutate(income = coalesce(income, median(income, na.rm = TRUE)))
05

Which should you hand over?

Reflect

Think of who runs this next.

  • An analyst rerunning this month's report on new data needs Refit in their language.
  • A colleague checking your published numbers needs Replay.
  • An engineer putting a model into production needs the Pipeline, so that values are learned from training rows.

Who is that person for your current work?

06

Check your understanding

Checkpoint

Check yourself

You send code to run on next quarter's data, and imputation should reflect that quarter. Which mode?

Show the answer

Answer: b

Replay would keep this quarter's median forever. Refit recalculates it.

Check yourself

Why are the scikit-learn and recipes targets always Refit?

Show the answer

Answer: a

Fit on training rows, apply elsewhere: that is what a pipeline is, and it is how leakage is avoided.

07

Recap

Recap
  • Code exports a recipe to SQL, pandas, dplyr, scikit-learn or recipes.
  • Replay reproduces the app; Refit recomputes on new data.
  • Pipelines are always Refit, because they fit on training rows.