-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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): Improve pl.when documentation #7793
Conversation
- clarify behaviour chained when-thens - note that otherwise is optional
Yes! That would be great. |
I propose we add an example of if..elif..else in the doc. Here's an example: import polars as pl
df = pl.DataFrame(
{'a': ['T', 'T', 'T', 'T', None],
'b': ['D', 'D', 'D', 'F', None]}
)
# this .when() chain ...
df.with_columns(
pl.when(pl.col('a') == 'T').then('T')
.when(pl.col('b') == 'F').then('F')
.otherwise('C').alias('c')
)
# ... is equal to this:
new_col_c = []
for row in df.to_dicts():
if row['a'] == 'T':
new_col_c.append('T')
elif row['b'] == 'F':
new_col_c.append('F')
else:
new_col_c.append('C')
print(pl.Series(new_col_c)) |
I have removed the See Also links, because they are not referring to objects you really deal with as an end-user. I have, however, kept in the docstrings on the class methods |
@StevenLi-DS : we have such an example? We dont have the fully written out Python alternative code, but I think I have added enough text to clarify what happens? |
@zundertj I think the docstrings and the doc are clear now. Thx. Maybe it's better to be inserted somewhere in the user guide. anyway... i don't know. |
Nice! |
Inspired by #7725
The See-Also section does not really work well, see the ref page here: https://pola-rs.github.io/polars/py-polars/html/reference/expressions/api/polars.when.html#polars.when
Because we do not explicitly expose the
WhenThen
andWhenThenThen
objects. Should we just remove that, and simply focus on documenting everything throughpl.when
? That would be my preference.