-
Notifications
You must be signed in to change notification settings - Fork 915
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle nans in groupby-aggregations in polars executor (#16233)
Polars `min` and `max` by default ignore nans (treating them as nulls), to mimic this behaviour we must mask out nans before performing a min/max aggregation. Do this by exposing `nans_to_nulls` in pylibcudf and implementing a `with_mask` method on pylibcudf Columns. Authors: - Lawrence Mitchell (https://github.com/wence-) Approvers: - Thomas Li (https://github.com/lithomas1) URL: #16233
- Loading branch information
Showing
18 changed files
with
230 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
========= | ||
transform | ||
========= | ||
|
||
.. automodule:: cudf._lib.pylibcudf.transform | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ set(cython_sources | |
sorting.pyx | ||
table.pyx | ||
traits.pyx | ||
transform.pyx | ||
types.pyx | ||
unary.pyx | ||
utils.pyx | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from .column cimport Column | ||
from .gpumemoryview cimport gpumemoryview | ||
|
||
|
||
cpdef tuple[gpumemoryview, int] nans_to_nulls(Column input) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from libcpp.memory cimport unique_ptr | ||
from libcpp.utility cimport move, pair | ||
|
||
from rmm._lib.device_buffer cimport DeviceBuffer, device_buffer | ||
|
||
from cudf._lib.pylibcudf.libcudf cimport transform as cpp_transform | ||
from cudf._lib.pylibcudf.libcudf.types cimport size_type | ||
|
||
from .column cimport Column | ||
from .gpumemoryview cimport gpumemoryview | ||
|
||
|
||
cpdef tuple[gpumemoryview, int] nans_to_nulls(Column input): | ||
"""Create a null mask preserving existing nulls and converting nans to null. | ||
Parameters | ||
---------- | ||
input : Column | ||
Column to produce new mask from. | ||
Returns | ||
------- | ||
Two-tuple of a gpumemoryview wrapping the null mask and the new null count. | ||
""" | ||
cdef pair[unique_ptr[device_buffer], size_type] c_result | ||
|
||
with nogil: | ||
c_result = move(cpp_transform.nans_to_nulls(input.view())) | ||
|
||
return ( | ||
gpumemoryview(DeviceBuffer.c_from_unique_ptr(move(c_result.first))), | ||
c_result.second | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
import math | ||
|
||
import pyarrow as pa | ||
from utils import assert_column_eq | ||
|
||
from cudf._lib import pylibcudf as plc | ||
|
||
|
||
def test_nans_to_nulls(has_nans): | ||
if has_nans: | ||
values = [1, float("nan"), float("nan"), None, 3, None] | ||
else: | ||
values = [1, 4, 5, None, 3, None] | ||
|
||
replaced = [ | ||
None if (v is None or (v is not None and math.isnan(v))) else v | ||
for v in values | ||
] | ||
|
||
h_input = pa.array(values, type=pa.float32()) | ||
input = plc.interop.from_arrow(h_input) | ||
assert input.null_count() == h_input.null_count | ||
expect = pa.array(replaced, type=pa.float32()) | ||
|
||
mask, null_count = plc.transform.nans_to_nulls(input) | ||
|
||
assert null_count == expect.null_count | ||
got = input.with_mask(mask, null_count) | ||
|
||
assert_column_eq(expect, got) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.