-
Notifications
You must be signed in to change notification settings - Fork 170
refactor: Enforce LazyExpr._from_elementwise_horizontal_op
#2718
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
Conversation
| @classmethod | ||
| def from_elementwise( | ||
| cls, func: Callable[[Iterable[NativeExprT]], NativeExprT], *exprs: Self | ||
| ) -> Self: ... |
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.
@MarcoGorelli would you be open to adding a little doc here? π
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.
Actually, another thought.
What about LazyExpr.from_horizontal_op?
That would mirror the same constructor these all use for ExprMetadata
narwhals/narwhals/functions.py
Lines 1369 to 1374 in f17acfc
| return Expr( | |
| lambda plx: apply_n_ary_operation( | |
| plx, plx.sum_horizontal, *flat_exprs, str_as_lit=False | |
| ), | |
| ExprMetadata.from_horizontal_op(*flat_exprs), | |
| ) |
narwhals/narwhals/_expression_parsing.py
Lines 477 to 481 in f17acfc
| @classmethod | |
| def from_horizontal_op(cls, *exprs: IntoExpr) -> ExprMetadata: | |
| return combine_metadata( | |
| *exprs, str_as_lit=False, allow_multi_output=True, to_single_output=True | |
| ) |
I think the detail that we should use this for *_horizontal (and concat_str) is more relevant than the elementwise part
| cls, func: Callable[[Iterable[Expression]], Expression], *exprs: Self | ||
| ) -> Self: | ||
| def call(df: DuckDBLazyFrame) -> list[Expression]: | ||
| cols = (col for _expr in exprs for col in _expr(df)) |
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.
I think these could just be:
return [func(chain.from_iterable(expr(df) for expr in exprs))]LazyExpr.from_elementwiseLazyExpr._from_horizontal_op
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.
thanks @dangotbanned
i've renamed and prefixed with an underscore as I don't think this should be part of the compliant protocols #2713, but fine for now
Thanks @MarcoGorelli! One thing though - the name in the PR title doesn't match the code π I prefer the one without elementwise |
LazyExpr._from_horizontal_opLazyExpr._from_elementwise_horizontal_op
|
thanks! same, but it does need to be elementwise if we use |

What type of PR is this? (check all applicable)
Related issues
Checklist
If you have comments or can explain your changes, please do so below
Having the method defined on the
Namespace(current), is what I was trying to avoid since_compliantsub-packageΒ #2149Prior to that, we had things like:
Namespace._create_series_from_scalarNamespace._create_expr_from_callableNamespace._create_expr_from_seriesI'd think it's much simpler to keep constructors defined on the class they produce, and they also benefit from shorter names π