-
Notifications
You must be signed in to change notification settings - Fork 182
feat: add DataFrame.top_k and LazyFrame.top_k
#2977
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
181a723
feat: add `top_k`
raisadz a1fb7c6
add docstrings, flatten `by`, use nlargest/nsmallest for pandas/dask β¦
raisadz 6cd961b
handle column names with empty space, sort test results
raisadz e920e66
Merge remote-tracking branch 'upstream/main' into feat/top_k
raisadz 6cee5de
check schema only for columns in `by`
raisadz d69f663
implement `top_k` for polars
raisadz 32d9708
fix dask
raisadz e2b76e1
add no cover for exception
raisadz 5c8ffdb
Update narwhals/_ibis/dataframe.py
raisadz 68a505f
Update narwhals/_duckdb/dataframe.py
raisadz 69f6bc5
Merge remote-tracking branch 'upstream/main' into feat/top_k
raisadz ad7ea79
address comments
raisadz cafb18d
Merge remote-tracking branch 'upstream/main' into feat/top_k
raisadz c57a406
add `nulls_last` for DuckDB, add a test with None
raisadz bb1f349
skip polars test for Nones
raisadz dbc0c72
Merge remote-tracking branch 'upstream/main' into feat/top_k
raisadz e45b151
replace `zip` with `zip_strict`
raisadz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,6 +43,7 @@ | |
| - shape | ||
| - sort | ||
| - tail | ||
| - top_k | ||
| - to_arrow | ||
| - to_dict | ||
| - to_native | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ | |
| - sink_parquet | ||
| - sort | ||
| - tail | ||
| - top_k | ||
| - to_native | ||
| - unique | ||
| - unpivot | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
|
|
||
| import narwhals as nw | ||
| from tests.utils import POLARS_VERSION, Constructor, assert_equal_data | ||
|
|
||
|
|
||
| def test_top_k(constructor: Constructor) -> None: | ||
| if "polars" in str(constructor) and POLARS_VERSION < (1, 0): | ||
| # old polars versions do not sort nulls last | ||
| pytest.skip() | ||
| data = {"a": ["a", "f", "a", "d", "b", "c"], "b c": [None, None, 2, 3, 6, 1]} | ||
| df = nw.from_native(constructor(data)) | ||
| result = df.top_k(4, by="b c") | ||
| expected = {"a": ["a", "b", "c", "d"], "b c": [2, 6, 1, 3]} | ||
| assert_equal_data(result.sort("a"), expected) | ||
| df = nw.from_native(constructor(data)) | ||
| result = df.top_k(4, by="b c", reverse=True) | ||
| expected = {"a": ["a", "b", "c", "d"], "b c": [2, 6, 1, 3]} | ||
| assert_equal_data(result.sort(by="a"), expected) | ||
|
|
||
|
|
||
| def test_top_k_by_multiple(constructor: Constructor) -> None: | ||
| data = { | ||
| "a": ["a", "f", "a", "d", "b", "c"], | ||
| "b": [2, 2, 2, 3, 1, 1], | ||
| "sf_c": ["k", "d", "s", "a", "a", "r"], | ||
| } | ||
| df = nw.from_native(constructor(data)) | ||
| result = df.top_k(4, by=["b", "sf_c"], reverse=True) | ||
| expected = { | ||
| "a": ["b", "f", "a", "c"], | ||
| "b": [1, 2, 2, 1], | ||
| "sf_c": ["a", "d", "k", "r"], | ||
| } | ||
| assert_equal_data(result.sort("sf_c"), expected) | ||
| data = { | ||
| "a": ["a", "f", "a", "d", "b", "c"], | ||
| "b": [2, 2, 2, 3, 1, 1], | ||
| "sf_c": ["k", "d", "s", "a", "a", "r"], | ||
| } | ||
| df = nw.from_native(constructor(data)) | ||
| result = df.top_k(4, by=["b", "sf_c"], reverse=[False, True]) | ||
| expected = { | ||
| "a": ["d", "f", "a", "a"], | ||
| "b": [3, 2, 2, 2], | ||
| "sf_c": ["a", "d", "k", "s"], | ||
| } | ||
| assert_equal_data(result.sort("sf_c"), expected) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a check that if
reverseis a sequence, and it's length is different thanflatten_by, then an exception is raise? This guarantees thatzip(by, reverse)at the compliant level is same aszip_strict.From polars:
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@raisadz I would still prefer to add a check at this level to also align the error with polars (notice that the output of
flattenis a list anyway), but feel free to merge. We can follow up on itThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think there's some other places where this would be useful (like
sort) so we could probably make a validation utility for this and use it in multiple places