-
Notifications
You must be signed in to change notification settings - Fork 207
feat: when-then broadcasting #2663
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
camriddell
merged 14 commits into
narwhals-dev:main
from
camriddell:feat-whenthen-broadcast
Jun 12, 2025
Merged
Changes from 6 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8e4489e
add when_then_broadcast tests
camriddell 51ad4d6
remove when-then expression_preserve_length check
camriddell 2bd2940
enh when-then eager backends
camriddell 3f69342
enh when-then broadcast dask backend
camriddell 2d4aa4a
_dask.expr.DaskExpr.broadcast workaround
camriddell 2f9edfc
fix get_dask_Expr import
camriddell 597b3af
Merge branch 'main' into feat-whenthen-broadcast
camriddell f02c7f0
when/then catch anticipated shape errors early
camriddell bd5dce3
add when/then shape and aggregation/broadcast tests
camriddell 283a017
swap the dask when/then broadcasting hack for a cleaner hack
camriddell 83e188b
when/then over pushdown in lazy backends
camriddell f4a7fba
cov when/then aggregation with None test
camriddell 4156549
dont use Dask private methods
MarcoGorelli fd16347
typo
MarcoGorelli 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
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 |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
|
|
||
| import operator | ||
| from functools import reduce | ||
| from typing import TYPE_CHECKING, Iterable, Sequence, cast | ||
| from typing import TYPE_CHECKING, Any, Iterable, Sequence, cast | ||
|
|
||
| import dask.dataframe as dd | ||
| import pandas as pd | ||
|
|
@@ -18,10 +18,12 @@ | |
| validate_comparand, | ||
| ) | ||
| from narwhals._expression_parsing import ( | ||
| ExprKind, | ||
| combine_alias_output_names, | ||
| combine_evaluate_output_names, | ||
| ) | ||
| from narwhals._utils import Implementation | ||
| from narwhals.dependencies import get_dask_expr | ||
|
|
||
| if TYPE_CHECKING: | ||
| import dask.dataframe.dask_expr as dx | ||
|
|
@@ -283,25 +285,44 @@ def _then(self) -> type[DaskThen]: | |
| return DaskThen | ||
|
|
||
| def __call__(self, df: DaskLazyFrame) -> Sequence[dx.Series]: | ||
| condition = self._condition(df)[0] | ||
|
|
||
| if isinstance(self._then_value, DaskExpr): | ||
| then_value = self._then_value(df)[0] | ||
| else: | ||
| then_value = self._then_value | ||
| (then_series,) = align_series_full_broadcast(df, then_value) | ||
| validate_comparand(condition, then_series) | ||
| def _aggregates(expr: Any) -> bool: | ||
| if isinstance(expr, DaskExpr): | ||
| return expr._metadata is not None and expr._metadata.is_scalar_like | ||
| elif isinstance(expr, dd.Series): | ||
| return len(expr) == 1 | ||
|
MarcoGorelli marked this conversation as resolved.
Outdated
|
||
| return True | ||
|
|
||
| then_value = ( | ||
| self._then_value(df)[0] | ||
| if isinstance(self._then_value, DaskExpr) | ||
| else self._then_value | ||
| ) | ||
| otherwise_value = ( | ||
| self._otherwise_value(df)[0] | ||
| if isinstance(self._otherwise_value, DaskExpr) | ||
| else self._otherwise_value | ||
| ) | ||
|
|
||
| if self._otherwise_value is None: | ||
| return [then_series.where(condition)] | ||
| otherwise_value = get_dask_expr()._expr.Where._defaults["other"] | ||
|
Member
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 the only part that jumps out to me as suspicious π€ |
||
|
|
||
| if isinstance(self._otherwise_value, DaskExpr): | ||
| otherwise_value = self._otherwise_value(df)[0] | ||
| else: | ||
| return [then_series.where(condition, self._otherwise_value)] # pyright: ignore[reportArgumentType] | ||
| (otherwise_series,) = align_series_full_broadcast(df, otherwise_value) | ||
| condition = self._condition(df)[0] | ||
| if ( | ||
| _aggregates(self._condition) | ||
| and _aggregates(then_value) | ||
| and _aggregates(otherwise_value) | ||
| ): | ||
| df = df._with_native(condition.to_frame()) | ||
| elif _aggregates(self._condition): | ||
| condition = self._condition.broadcast(ExprKind.AGGREGATION)(df)[0] | ||
|
|
||
| (condition, then_series, otherwise_series) = align_series_full_broadcast( | ||
| df, condition, then_value, otherwise_value | ||
| ) | ||
|
|
||
| validate_comparand(condition, then_series) | ||
| validate_comparand(condition, otherwise_series) | ||
| return [then_series.where(condition, otherwise_series)] # pyright: ignore[reportArgumentType] | ||
| return [then_series.where(condition, otherwise_series)] | ||
|
|
||
|
|
||
| class DaskThen(CompliantThen[DaskLazyFrame, "dx.Series", DaskExpr], DaskExpr): ... | ||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.