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: Fix aggregation guide discrepancies #18003

Merged
merged 3 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 1 addition & 3 deletions docs/src/python/user-guide/expressions/aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,7 @@ def get_person() -> pl.Expr:
get_person().first().alias("youngest"),
get_person().last().alias("oldest"),
get_person().sort().first().alias("alphabetical_first"),
pl.col("gender")
.sort_by(pl.col("first_name").cast(pl.Categorical("lexical")))
.first(),
pl.col("gender").sort_by(get_person()).first(),
)
.sort("state")
.limit(5)
Expand Down
7 changes: 3 additions & 4 deletions docs/user-guide/expressions/aggregation.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ Per GROUP `"first_name"` we
<!-- dprint-ignore-start -->

- count the number of rows in the group:
- short form: `pl.count("party")`
- full form: `pl.col("party").count()`
- aggregate the gender values groups:
- full form: `pl.len()`
- combine the values of gender into a list by omitting an aggregate function:
- full form: `pl.col("gender")`
- get the first value of column `"last_name"` in the group:
- short form: `pl.first("last_name")` (not available in Rust)
Expand Down Expand Up @@ -94,7 +93,7 @@ However, **if** we also want to sort the names alphabetically, this breaks. Luck
--8<-- "python/user-guide/expressions/aggregation.py:sort2"
```

We can even sort by another column in the `group_by` context. If we want to know if the alphabetically sorted name is male or female we could add: `pl.col("gender").sort_by("first_name").first().alias("gender")`
We can even sort by another column in the `group_by` context. If we want to know if the alphabetically sorted name is male or female we could add: `pl.col("gender").sort_by(get_person()).first()`

{{code_block('user-guide/expressions/aggregation','sort3',['group_by'])}}

Expand Down