Initializing playground…
← Back to roadmap

case_when

Conditional value selection using CASE WHEN semantics — mirrors pandas.Series.case_when() (pandas 2.2+).

1 — Basic grade classification

caseWhen(series, caselist) applies an ordered list of [condition, replacement] pairs. The first matching condition determines the output; if no condition matches the original value is kept.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

2 — Using boolean Series as conditions

Conditions can be boolean Series objects (e.g. from comparison operations).

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

3 — Using predicate functions

Conditions can be predicate functions (value, index) => boolean.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

4 — Series as replacement values

Replacements can be Series objects — the matching positional value is used.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

5 — Unmatched rows keep original values

Any row not matched by any condition retains its original value — there is no implicit "else" replacement.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

6 — First matching condition wins

When multiple conditions match the same row, the first one in caselist takes effect — just like CASE WHEN … THEN … WHEN … THEN … END in SQL.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

7 — Positional index in predicate

Predicate functions receive both the value and its positional index as the second argument.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

8 — String Series classification

caseWhen works on any Series type — numbers, strings, booleans, or mixed.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

9 — Comparison with where / mask

caseWhen generalises whereSeries to multiple branches. Use whereSeries for a single condition; use caseWhen for multi-branch logic.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent