Skip to content
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

feat: add all, any and null_count Spark Expressions #1724

Merged
merged 19 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions narwhals/_spark_like/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,16 @@ def _alias(df: SparkLikeLazyFrame) -> list[Column]:
kwargs={**self._kwargs, "name": name},
)

def all(self) -> Self:
from pyspark.sql import functions as F # noqa: N812

return self._from_call(F.bool_and, "all", returns_scalar=True)

def any(self) -> Self:
from pyspark.sql import functions as F # noqa: N812

return self._from_call(F.bool_or, "any", returns_scalar=True)

def count(self) -> Self:
from pyspark.sql import functions as F # noqa: N812

Expand Down Expand Up @@ -306,6 +316,14 @@ def min(self) -> Self:

return self._from_call(F.min, "min", returns_scalar=True)

def null_count(self) -> Self:
def _null_count(_input: Column) -> Column:
from pyspark.sql import functions as F # noqa: N812

return F.count_if(F.isnull(_input))

return self._from_call(_null_count, "null_count", returns_scalar=True)

def sum(self) -> Self:
from pyspark.sql import functions as F # noqa: N812

Expand Down
9 changes: 2 additions & 7 deletions tests/expr_and_series/any_all_test.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
from __future__ import annotations

import pytest

import narwhals.stable.v1 as nw
from tests.utils import Constructor
from tests.utils import ConstructorEager
from tests.utils import assert_equal_data


def test_any_all(request: pytest.FixtureRequest, constructor: Constructor) -> None:
if "pyspark" in str(constructor):
request.applymarker(pytest.mark.xfail)

def test_any_all(constructor: Constructor) -> None:
df = nw.from_native(
constructor(
{
Expand All @@ -24,7 +19,7 @@ def test_any_all(request: pytest.FixtureRequest, constructor: Constructor) -> No
result = df.select(nw.col("a", "b", "c").all())
expected = {"a": [False], "b": [True], "c": [False]}
assert_equal_data(result, expected)
result = df.select(nw.all().any())
result = df.select(nw.col("a", "b", "c").any())
Comment on lines -27 to +22
Copy link
Member

@FBruzzesi FBruzzesi Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MarcoGorelli pyspark fails to maintain the original name with nw.all().<expression_method>. @EdAbati maybe you know why and where this is happening already.

Here it would result in:

FAILED tests/expr_and_series/any_all_test.py::test_any_all[pyspark] - AssertionError: Expected column name a at index 0, found bool_or(a)

expected = {"a": [True], "b": [True], "c": [False]}
assert_equal_data(result, expected)

Expand Down
4 changes: 2 additions & 2 deletions tests/expr_and_series/null_count_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
def test_null_count_expr(
constructor: Constructor, request: pytest.FixtureRequest
) -> None:
if ("pyspark" in str(constructor)) or "duckdb" in str(constructor):
if "duckdb" in str(constructor):
request.applymarker(pytest.mark.xfail)
df = nw.from_native(constructor(data))
result = df.select(nw.all().null_count())
result = df.select(nw.col("a", "b").null_count())
expected = {
"a": [2],
"b": [1],
Expand Down
Loading