-
Notifications
You must be signed in to change notification settings - Fork 159
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
@@ -1,11 +1,13 @@ | ||
from __future__ import annotations | ||
|
||
from functools import reduce | ||
from typing import TYPE_CHECKING | ||
from typing import Any | ||
from typing import Iterable | ||
from typing import Sequence | ||
|
||
from narwhals import dtypes | ||
from narwhals._expression_parsing import parse_into_exprs | ||
from narwhals._polars.utils import extract_args_kwargs | ||
from narwhals._polars.utils import narwhals_to_native_dtype | ||
from narwhals.dependencies import get_polars | ||
|
@@ -15,6 +17,7 @@ | |
from narwhals._polars.dataframe import PolarsDataFrame | ||
from narwhals._polars.dataframe import PolarsLazyFrame | ||
from narwhals._polars.expr import PolarsExpr | ||
from narwhals._polars.typing import IntoPolarsExpr | ||
|
||
|
||
class PolarsNamespace: | ||
|
@@ -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: | ||
from narwhals._polars.expr import PolarsExpr | ||
|
||
pl = get_polars() | ||
if self._backend_version < (0, 20, 4): # pragma: no cover | ||
return pl.mean([*column_names]) | ||
return pl.mean(*column_names) | ||
return PolarsExpr(pl.mean([*column_names])) | ||
return PolarsExpr(pl.mean(*column_names)) | ||
|
||
def mean_horizontal(self, *exprs: IntoPolarsExpr) -> PolarsExpr: | ||
from narwhals._polars.expr import PolarsExpr | ||
|
||
pl = get_polars() | ||
polars_exprs = parse_into_exprs(*exprs, namespace=self) | ||
|
||
if self._backend_version < (0, 20, 8): # pragma: no cover | ||
total = reduce(lambda x, y: x + y, (e.fill_null(0.0) for e in polars_exprs)) | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
return PolarsExpr(pl.mean_horizontal([e._native_expr for e in polars_exprs])) | ||
|
||
@property | ||
def selectors(self) -> PolarsSelectors: | ||
|
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,17 @@ | ||
from __future__ import annotations # pragma: no cover | ||
|
||
from typing import TYPE_CHECKING # pragma: no cover | ||
from typing import Union # pragma: no cover | ||
|
||
if TYPE_CHECKING: | ||
import sys | ||
|
||
if sys.version_info >= (3, 10): | ||
from typing import TypeAlias | ||
else: | ||
from typing_extensions import TypeAlias | ||
|
||
from narwhals._polars.expr import PolarsExpr | ||
from narwhals._polars.series import PolarsSeries | ||
|
||
IntoPolarsExpr: TypeAlias = Union[PolarsExpr, str, PolarsSeries] |
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,15 @@ | ||
from typing import Any | ||
|
||
import pytest | ||
|
||
import narwhals.stable.v1 as nw | ||
from tests.utils import compare_dicts | ||
|
||
|
||
@pytest.mark.parametrize("col_expr", [nw.col("a"), "a"]) | ||
def test_meanh(constructor: Any, col_expr: Any) -> None: | ||
data = {"a": [1, 3, None, None], "b": [4, None, 6, None]} | ||
df = nw.from_native(constructor(data)) | ||
result = df.select(horizontal_mean=nw.mean_horizontal(col_expr, nw.col("b"))) | ||
expected = {"horizontal_mean": [2.5, 3.0, 6.0, float("nan")]} | ||
compare_dicts(result, 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.
We should return
PolarsExpr
to be able to keep chaining and interacting with other narwhals exprs if needed