Skip to content

Commit a2c4502

Browse files
committed
Support pl.Expr.is_empty in cudf_polars
1 parent 15eb369 commit a2c4502

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

python/cudf_polars/cudf_polars/dsl/expressions/boolean.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ def __init__(
9999
BooleanFunction.Name.All,
100100
BooleanFunction.Name.Any,
101101
BooleanFunction.Name.IsDuplicated,
102+
BooleanFunction.Name.IsEmpty,
102103
BooleanFunction.Name.IsFirstDistinct,
103104
BooleanFunction.Name.IsLastDistinct,
104105
BooleanFunction.Name.IsSorted,
@@ -107,7 +108,6 @@ def __init__(
107108
if self.name in {
108109
BooleanFunction.Name.HasNulls,
109110
BooleanFunction.Name.IsClose,
110-
BooleanFunction.Name.IsEmpty,
111111
}:
112112
raise NotImplementedError(
113113
f"Boolean function {self.name}"
@@ -192,6 +192,19 @@ def do_evaluate(
192192
self, df: DataFrame, *, context: ExecutionContext = ExecutionContext.FRAME
193193
) -> Column:
194194
"""Evaluate this expression given a dataframe for context."""
195+
if self.name is BooleanFunction.Name.IsEmpty:
196+
(child,) = self.children
197+
column = child.evaluate(df, context=context)
198+
return Column(
199+
plc.Column.from_scalar(
200+
plc.Scalar.from_py(
201+
column.size == 0, self.dtype.plc_type, stream=df.stream
202+
),
203+
1,
204+
stream=df.stream,
205+
),
206+
dtype=self.dtype,
207+
)
195208
if self.name in (
196209
BooleanFunction.Name.IsFinite,
197210
BooleanFunction.Name.IsInfinite,

python/cudf_polars/tests/expressions/test_booleanfunction.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
assert_gpu_result_equal,
1313
assert_ir_translation_raises,
1414
)
15-
from cudf_polars.utils.versions import POLARS_VERSION_LT_142
15+
from cudf_polars.utils.versions import (
16+
POLARS_VERSION_LT_141,
17+
POLARS_VERSION_LT_142,
18+
)
1619

1720
if TYPE_CHECKING:
1821
from collections.abc import Callable
@@ -282,6 +285,17 @@ def test_boolean_is_close(engine: pl.GPUEngine):
282285
assert_ir_translation_raises(q, engine, NotImplementedError)
283286

284287

288+
@pytest.mark.skipif(
289+
POLARS_VERSION_LT_141,
290+
reason="has_nulls/is_empty added to polars' BooleanFunction in 1.41",
291+
)
292+
@pytest.mark.parametrize("data", [[1, 2, 3], [None, None], []])
293+
def test_boolean_is_empty(engine: pl.GPUEngine, data):
294+
ldf = pl.LazyFrame({"a": pl.Series(data, dtype=pl.Int64)})
295+
q = ldf.select(pl.col("a").is_empty())
296+
assert_gpu_result_equal(q, engine=engine)
297+
298+
285299
@pytest.mark.parametrize(
286300
"dtype, col",
287301
[

0 commit comments

Comments
 (0)