Skip to content

Commit 947ecd9

Browse files
committed
Support pl.Expr.drop_nans in cudf_polars
1 parent 15eb369 commit 947ecd9

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class UnaryFunction(Expr):
104104
_supported_misc_fns = frozenset(
105105
{
106106
"as_struct",
107+
"drop_nans",
107108
"drop_nulls",
108109
"fill_null",
109110
"fill_null_with_strategy",
@@ -329,6 +330,16 @@ def do_evaluate(
329330
[keys_col, counts_col],
330331
)
331332
return Column(plc_column, dtype=self.dtype)
333+
elif self.name == "drop_nans":
334+
(column,) = (child.evaluate(df, context=context) for child in self.children)
335+
if not plc.traits.is_floating_point(column.obj.type()):
336+
return column
337+
return Column(
338+
plc.stream_compaction.drop_nans(
339+
plc.Table([column.obj]), [0], 1, stream=df.stream
340+
).columns()[0],
341+
dtype=self.dtype,
342+
)
332343
elif self.name == "drop_nulls":
333344
(column,) = (child.evaluate(df, context=context) for child in self.children)
334345
if column.null_count == 0:
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
from __future__ import annotations
4+
5+
import pytest
6+
7+
import polars as pl
8+
9+
from cudf_polars.testing.asserts import assert_gpu_result_equal
10+
11+
12+
@pytest.mark.parametrize(
13+
"series",
14+
[
15+
pl.Series("a", [1.0, float("nan"), 3.0, None, float("nan")]),
16+
pl.Series("a", [1.0, 2.0, 3.0]),
17+
pl.Series("a", [float("nan"), float("nan")]),
18+
pl.Series("a", [None, None], dtype=pl.Float64),
19+
pl.Series("a", [1, 2, None, 3], dtype=pl.Int64),
20+
pl.Series("a", [], dtype=pl.Float64),
21+
],
22+
)
23+
def test_drop_nans(engine: pl.GPUEngine, series: pl.Series) -> None:
24+
lf = pl.LazyFrame({"a": series})
25+
q = lf.select(pl.col("a").drop_nans())
26+
assert_gpu_result_equal(q, engine=engine)

0 commit comments

Comments
 (0)