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 is_nan and is_finite for duckdb, is_nan for pyspark #1825

Merged
merged 3 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/_duckdb/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,24 @@ def is_null(self) -> Self:
lambda _input: _input.isnull(), "is_null", returns_scalar=self._returns_scalar
)

def is_nan(self) -> Self:
from duckdb import FunctionExpression

return self._from_call(
lambda _input: FunctionExpression("isnan", _input),
"is_nan",
returns_scalar=self._returns_scalar,
)

def is_finite(self) -> Self:
from duckdb import FunctionExpression

return self._from_call(
lambda _input: FunctionExpression("isfinite", _input),
"is_finite",
returns_scalar=self._returns_scalar,
)

def is_in(self, other: Sequence[Any]) -> Self:
from duckdb import ConstantExpression

Expand Down
8 changes: 8 additions & 0 deletions narwhals/_spark_like/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,14 @@ def is_null(self: Self) -> Self:

return self._from_call(F.isnull, "is_null", returns_scalar=self._returns_scalar)

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

def _is_nan(_input: Column) -> Column:
return F.when(F.isnull(_input), None).otherwise(F.isnan(_input))

return self._from_call(_is_nan, "is_nan", returns_scalar=self._returns_scalar)

@property
def str(self: Self) -> SparkLikeExprStringNamespace:
return SparkLikeExprStringNamespace(self)
Expand Down
13 changes: 5 additions & 8 deletions tests/expr_and_series/is_finite_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,12 @@


@pytest.mark.filterwarnings("ignore:invalid value encountered in cast")
def test_is_finite_expr(constructor: Constructor, request: pytest.FixtureRequest) -> None:
if ("pyspark" in str(constructor)) or "duckdb" in str(constructor):
request.applymarker(pytest.mark.xfail)
if "polars" in str(constructor) or "pyarrow_table" in str(constructor):
def test_is_finite_expr(constructor: Constructor) -> None:
if any(x in str(constructor) for x in ("polars", "pyarrow_table", "duckdb")):
expected = {"a": [False, False, True, None]}
elif (
"pandas_constructor" in str(constructor)
or "dask" in str(constructor)
or "modin_constructor" in str(constructor)
elif any(
x in str(constructor)
for x in ("pandas_constructor", "dask", "modin_constructor", "pyspark")
FBruzzesi marked this conversation as resolved.
Show resolved Hide resolved
):
expected = {"a": [False, False, True, False]}
else: # pandas_nullable_constructor, pandas_pyarrow_constructor, modin_pyarrrow_constructor
Expand Down
8 changes: 3 additions & 5 deletions tests/expr_and_series/is_nan_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@
]


def test_nan(constructor: Constructor, request: pytest.FixtureRequest) -> None:
if ("pyspark" in str(constructor)) or "duckdb" in str(constructor):
request.applymarker(pytest.mark.xfail)
data_na = {"int": [0, 1, None]}
def test_nan(constructor: Constructor) -> None:
data_na = {"int": [-1, 1, None]}
df = nw.from_native(constructor(data_na)).with_columns(
float=nw.col("int").cast(nw.Float64), float_na=nw.col("int") / nw.col("int")
float=nw.col("int").cast(nw.Float64), float_na=nw.col("int") ** 0.5
Comment on lines -32 to +30
Copy link
Member

Choose a reason for hiding this comment

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

why does this need changing?

Copy link
Member Author

@FBruzzesi FBruzzesi Jan 19, 2025

Choose a reason for hiding this comment

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

Sorry I forgot to comment, and it's a fun one.

Apparently, zero division leads to Null, while the square root of -1 is NaN in pyspark πŸ˜…

)
result = df.select(
int=nw.col("int").is_nan(),
Expand Down
Loading