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): Improve pl.when documentation #7793

Merged
merged 2 commits into from
Mar 28, 2023

Conversation

zundertj
Copy link
Collaborator

@zundertj zundertj commented Mar 26, 2023

Inspired by #7725

  • clarify behaviour chained when-thens
  • note that otherwise is optional

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 and WhenThenThen objects. Should we just remove that, and simply focus on documenting everything through pl.when? That would be my preference.

- clarify behaviour chained when-thens
- note that otherwise is optional
@github-actions github-actions bot added documentation Improvements or additions to documentation python Related to Python Polars labels Mar 26, 2023
@stinodego
Copy link
Contributor

Because we do not explicitly expose the WhenThen and WhenThenThen objects. Should we just remove that, and simply focus on documenting everything through pl.when? That would be my preference.

Yes! That would be great.

@stevenlis
Copy link

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))

@zundertj
Copy link
Collaborator Author

zundertj commented Mar 27, 2023

Because we do not explicitly expose the WhenThen and WhenThenThen objects. Should we just remove that, and simply focus on documenting everything through pl.when? That would be my preference.

Yes! That would be great.

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 when, then, otherwise, useful in IDE's.

@zundertj
Copy link
Collaborator Author

@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?

@stevenlis
Copy link

@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.

@ritchie46
Copy link
Member

Nice!

@ritchie46 ritchie46 merged commit 2cec353 into pola-rs:master Mar 28, 2023
LdRoW pushed a commit to LdRoW/polars that referenced this pull request Mar 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation python Related to Python Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants