-
Notifications
You must be signed in to change notification settings - Fork 141
feat: mean_horizontal
#843
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
Ok, so this ended up being a bit more than initially expected on the polars side 😂 |
@@ -85,11 +88,28 @@ def lit(self, value: Any, dtype: dtypes.DType | None = None) -> PolarsExpr: | |||
return PolarsExpr(pl.lit(value, dtype=narwhals_to_native_dtype(dtype))) | |||
return PolarsExpr(pl.lit(value)) | |||
|
|||
def mean(self, *column_names: str) -> Any: | |||
def mean(self, *column_names: str) -> PolarsExpr: |
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.
We should return PolarsExpr
to be able to keep chaining and interacting with other narwhals exprs if needed
n_non_zero = reduce( | ||
lambda x, y: x + y, ((1 - e.is_null()) for e in polars_exprs) | ||
) | ||
return PolarsExpr(total._native_expr / n_non_zero._native_expr) |
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.
__div__
is not currently implemented and I did not want to make the PR even larger
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.
this is wonderful, thanks @FBruzzesi !
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.
As
mean
can only be numeric, unlike #825, it is safe to fill nulls with zeros for the sum, and count the number of non-nulls summands.