Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(python): Document chained when-then behaviour more prominently #10759

Merged
merged 2 commits into from
Aug 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions py-polars/polars/functions/whenthen.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ def when(condition: IntoExpr) -> pl.When:

Expression similar to an `if-else` statement in Python. Always initiated by a
`pl.when(<condition>).then(<value if condition>)`. Optionally followed by chaining
one or more `.when(<condition>).then(<value>)` statements. If none of the conditions
are `True`, an optional `.otherwise(<value if all statements are false>)` can be
appended at the end. If not appended, and none of the conditions are `True`, `None`
will be returned.
one or more `.when(<condition>).then(<value>)` statements.

Chained `when, thens` should be read as Python `if, elif, ... elif` blocks, not as
`if, if, ... if`, i.e. the first condition that evaluates to True will be picked.

If none of the conditions are `True`, an optional `.otherwise(<value if all
statements are false>)` can be appended at the end. If not appended, and none
of the conditions are `True`, `None` will be returned.

Parameters
----------
Expand Down Expand Up @@ -82,9 +86,7 @@ def when(condition: IntoExpr) -> pl.When:
│ 4 ┆ 0 ┆ 1 │
└─────┴─────┴─────┘

Chained `when, thens` should be read as `if, elif, ... elif`, not
as `if, if, ... if`, i.e. the first condition that evaluates to True will
be picked. Note how in the example above for the second row in the dataframe,
Note how in the example above for the second row in the dataframe,
where `foo=3` and `bar=4`, the first `when` evaluates to `True`, and therefore
the second `when`, which is also `True`, is not evaluated.

Expand Down