-
Notifications
You must be signed in to change notification settings - Fork 174
feat: Add {Expr,Series}.any_value
#3315
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
Open
FBruzzesi
wants to merge
10
commits into
main
Choose a base branch
from
feat/any_value
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b21a84f
WIP: Eager
FBruzzesi 25bb785
sql-like backends
FBruzzesi c050e26
docstrings
FBruzzesi 30611f6
test over, fail dask
FBruzzesi 777d7f8
xfail dask
FBruzzesi 3e14c11
merge main
FBruzzesi 914f505
skip old duckdb, simplify _sql since aggregation
FBruzzesi 7a4c938
rm seed, add ignore_nulls argument
FBruzzesi 7906cc2
pandas and tests
FBruzzesi 0eca51c
fix up
FBruzzesi 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 |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| - alias | ||
| - all | ||
| - any | ||
| - any_value | ||
| - cast | ||
| - ceil | ||
| - clip | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
| - alias | ||
| - all | ||
| - any | ||
| - any_value | ||
| - arg_max | ||
| - arg_min | ||
| - arg_true | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -109,7 +109,13 @@ def window_kwargs_to_pandas_equivalent( # noqa: C901 | |
| "min_periods": kwargs["min_samples"], | ||
| "ignore_na": kwargs["ignore_nulls"], | ||
| } | ||
| elif function_name in {"first", "last"}: | ||
| elif function_name in {"first", "last", "any_value"}: | ||
| if kwargs.get("ignore_nulls"): | ||
| msg = ( | ||
| "`Expr.any_value(ignore_nulls=True)` is not supported in a `over` " | ||
| "context for pandas-like backend." | ||
| ) | ||
| raise NotImplementedError(msg) | ||
|
Comment on lines
+113
to
+118
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an issue if a group has all null values |
||
| pandas_kwargs = { | ||
| "n": _REMAP_ORDERED_INDEX[cast("NarwhalsAggregation", function_name)] | ||
| } | ||
|
|
@@ -357,17 +363,16 @@ def func(df: PandasLikeDataFrame) -> Sequence[PandasLikeSeries]: # noqa: C901, | |
| msg = "Safety check failed, please report a bug." | ||
| raise AssertionError(msg) | ||
| res_native = grouped.transform("size").to_frame(aliases[0]) | ||
| elif function_name in {"first", "last"}: | ||
| elif function_name in {"first", "last", "any_value"}: | ||
| with warnings.catch_warnings(): | ||
| # Ignore settingwithcopy warnings/errors, they're false-positives here. | ||
| warnings.filterwarnings("ignore", message="\n.*copy of a slice") | ||
| _nth = getattr( | ||
| _agg = getattr( | ||
| grouped[[*partition_by, *aliases]], pandas_function_name | ||
| )(**pandas_kwargs) | ||
| _nth.reset_index(drop=True, inplace=True) | ||
| res_native = df.native[list(partition_by)].merge( | ||
| _nth, on=list(partition_by) | ||
| )[list(aliases)] | ||
| _agg.reset_index(drop=True, inplace=True) | ||
| keys = list(partition_by) | ||
| res_native = df.native[keys].merge(_agg, on=keys)[list(aliases)] | ||
| else: | ||
| res_native = grouped[list(aliases)].transform( | ||
| pandas_function_name, **pandas_kwargs | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -62,6 +62,7 @@ | |
| _REMAP_ORDERED_INDEX: Mapping[NarwhalsAggregation, Literal[0, -1]] = { | ||
| "first": 0, | ||
| "last": -1, | ||
| "any_value": 0, | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -151,7 +152,7 @@ def _getitem_aggs( | |
| for col in cols | ||
| ] | ||
| ) | ||
| elif self.is_last() or self.is_first(): | ||
| elif self.is_last() or self.is_first() or self.is_any_value(): | ||
| result = self.native_agg()(group_by._grouped[[*group_by._keys, *names]]) | ||
| result.set_index(group_by._keys, inplace=True) # noqa: PD002 | ||
| else: | ||
|
|
@@ -175,6 +176,9 @@ def is_first(self) -> bool: | |
| def is_mode(self) -> bool: | ||
| return self.leaf_name == "mode" | ||
|
|
||
| def is_any_value(self) -> bool: | ||
| return self.leaf_name == "any_value" | ||
|
|
||
| def is_top_level_function(self) -> bool: | ||
| # e.g. `nw.len()`. | ||
| return len(list(self.expr._metadata.op_nodes_reversed())) == 1 | ||
|
|
@@ -191,6 +195,12 @@ def native_agg(self) -> _NativeAgg: | |
| native_name = PandasLikeGroupBy._remap_expr_name(self.leaf_name) | ||
| last_node = next(self.expr._metadata.op_nodes_reversed()) | ||
| if self.leaf_name in _REMAP_ORDERED_INDEX: | ||
| if last_node.kwargs.get("ignore_nulls"): | ||
| msg = ( | ||
| "`Expr.any_value(ignore_nulls=True)` is not supported in a `group_by` " | ||
| "context for pandas-like backend" | ||
| ) | ||
| raise NotImplementedError(msg) | ||
|
Comment on lines
+198
to
+203
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an issue if a group has all null values |
||
| return methodcaller("nth", n=_REMAP_ORDERED_INDEX[self.leaf_name]) | ||
| return _native_agg(native_name, **last_node.kwargs) | ||
|
|
||
|
|
@@ -215,6 +225,7 @@ class PandasLikeGroupBy( | |
| "any": "any", | ||
| "first": "nth", | ||
| "last": "nth", | ||
| "any_value": "nth", | ||
| } | ||
| _original_columns: tuple[str, ...] | ||
| """Column names *prior* to any aliasing in `ParseKeysGroupBy`.""" | ||
|
|
||
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
Oops, something went wrong.
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.
expr.first(include_null=not ignore_nulls)is not behaving as expected π«