From cc3e72205b180431e2112d0b8859ab552f29a403 Mon Sep 17 00:00:00 2001 From: FBruzzesi Date: Fri, 1 Nov 2024 19:33:27 +0100 Subject: [PATCH] add dask and test --- narwhals/_dask/expr.py | 9 +++++++++ tests/expr_and_series/is_finite_test.py | 27 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 tests/expr_and_series/is_finite_test.py diff --git a/narwhals/_dask/expr.py b/narwhals/_dask/expr.py index db29f6c4d..0adb58814 100644 --- a/narwhals/_dask/expr.py +++ b/narwhals/_dask/expr.py @@ -725,6 +725,15 @@ def func(_input: Any, dtype: DType | type[DType]) -> Any: returns_scalar=False, ) + def is_finite(self: Self) -> Self: + import dask.array as da # ignore-banned-import + + return self._from_call( + lambda _input: da.isfinite(_input), + "is_finite", + returns_scalar=False, + ) + class DaskExprStringNamespace: def __init__(self, expr: DaskExpr) -> None: diff --git a/tests/expr_and_series/is_finite_test.py b/tests/expr_and_series/is_finite_test.py new file mode 100644 index 000000000..6fee3311d --- /dev/null +++ b/tests/expr_and_series/is_finite_test.py @@ -0,0 +1,27 @@ +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 + +data = {"a": [float("nan"), float("inf"), 2.0]} +expected = {"a": [False, False, True]} + + +@pytest.mark.filterwarnings("ignore:invalid value encountered in cast") +def test_is_finite_expr(constructor: Constructor) -> None: + df = nw.from_native(constructor(data)) + result = df.select(nw.col("a").is_finite()) + + assert_equal_data(result, expected) + + +@pytest.mark.filterwarnings("ignore:invalid value encountered in cast") +def test_is_finite_series(constructor_eager: ConstructorEager) -> None: + df = nw.from_native(constructor_eager(data), eager_only=True) + result = {"a": df["a"].is_finite()} + + assert_equal_data(result, expected)