Skip to content

Commit e342362

Browse files
committed
Support pl.Expr.cum_count in cudf_polars
1 parent 15eb369 commit e342362

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

python/cudf_polars/cudf_polars/dsl/expressions/unary.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ class UnaryFunction(Expr):
121121
)
122122
_supported_cum_aggs = frozenset(
123123
{
124+
"cum_count",
124125
"cum_min",
125126
"cum_max",
126127
"cum_prod",
@@ -552,6 +553,22 @@ def do_evaluate(
552553
)
553554
elif self.name in UnaryFunction._supported_cum_aggs:
554555
column = self.children[0].evaluate(df, context=context)
556+
if self.name == "cum_count":
557+
# cum_count is the cumulative count of non-null values.
558+
counts = plc.unary.cast(
559+
plc.unary.is_valid(column.obj, stream=df.stream),
560+
self.dtype.plc_type,
561+
stream=df.stream,
562+
)
563+
return Column(
564+
plc.reduce.scan(
565+
counts,
566+
plc.aggregation.sum(),
567+
plc.reduce.ScanType.INCLUSIVE,
568+
stream=df.stream,
569+
),
570+
dtype=self.dtype,
571+
)
555572
plc_col = column.obj
556573
col_type = column.dtype.plc_type
557574
# cum_sum casts

python/cudf_polars/tests/expressions/test_agg.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION & AFFILIATES.
1+
# SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0
33
from __future__ import annotations
44

@@ -34,6 +34,7 @@
3434
"std",
3535
"var",
3636
# scan aggs from UnaryFunction
37+
"cum_count",
3738
"cum_min",
3839
"cum_max",
3940
"cum_prod",
@@ -112,6 +113,21 @@ def test_bool_agg(engine: pl.GPUEngine, agg):
112113
assert_gpu_result_equal(q, engine=engine, check_exact=False)
113114

114115

116+
@pytest.mark.parametrize(
117+
"data",
118+
[
119+
[1, None, 3, None, 5],
120+
[None, None, None],
121+
[1, 2, 3],
122+
[],
123+
],
124+
)
125+
def test_cum_count(engine: pl.GPUEngine, data):
126+
df = pl.LazyFrame({"a": pl.Series(data, dtype=pl.Int64())})
127+
q = df.select(pl.col("a").cum_count())
128+
assert_gpu_result_equal(q, engine=engine)
129+
130+
115131
@pytest.mark.parametrize("cum_agg", sorted(expr.UnaryFunction._supported_cum_aggs))
116132
def test_cum_agg_reverse_unsupported(engine: pl.GPUEngine, cum_agg):
117133
df = pl.LazyFrame({"a": [1, 2, 3]})

0 commit comments

Comments
 (0)