Skip to content

Commit

Permalink
docs(python): Document chained when-then behaviour more prominently (#…
Browse files Browse the repository at this point in the history
…10759)

Co-authored-by: Stijn de Gooijer <stijn@degooijer.io>
  • Loading branch information
zundertj and stinodego authored Aug 29, 2023
1 parent d2900de commit 18847fb
Showing 1 changed file with 9 additions and 7 deletions.
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

0 comments on commit 18847fb

Please sign in to comment.