-
Notifications
You must be signed in to change notification settings - Fork 908
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
xref #15162 Migrate round.pxd to use pylibcudf APIs. Authors: - Thomas Li (https://github.com/lithomas1) Approvers: - https://github.com/brandon-b-miller - Lawrence Mitchell (https://github.com/wence-) URL: #15863
- Loading branch information
Showing
12 changed files
with
134 additions
and
27 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 @@ | ||
===== | ||
round | ||
===== | ||
|
||
.. automodule:: cudf._lib.pylibcudf.round | ||
: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
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
Empty file.
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,13 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
from libc.stdint cimport int32_t | ||
|
||
from cudf._lib.pylibcudf.libcudf.round cimport rounding_method | ||
|
||
from .column cimport Column | ||
|
||
|
||
cpdef Column round( | ||
Column source, | ||
int32_t decimal_places = *, | ||
rounding_method round_method = * | ||
) |
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,54 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
from libc.stdint cimport int32_t | ||
from libcpp.memory cimport unique_ptr | ||
from libcpp.utility cimport move | ||
|
||
from cudf._lib.pylibcudf.libcudf.round cimport ( | ||
round as cpp_round, | ||
rounding_method, | ||
) | ||
|
||
from cudf._lib.pylibcudf.libcudf.round import \ | ||
rounding_method as RoundingMethod # no-cython-lint | ||
|
||
from cudf._lib.pylibcudf.libcudf.column.column cimport column | ||
|
||
from .column cimport Column | ||
|
||
|
||
cpdef Column round( | ||
Column source, | ||
int32_t decimal_places = 0, | ||
rounding_method round_method = rounding_method.HALF_UP | ||
): | ||
"""Rounds all the values in a column to the specified number of decimal places. | ||
For details, see :cpp:func:`round`. | ||
Parameters | ||
---------- | ||
source : Column | ||
The Column for which to round values. | ||
decimal_places: int32_t, optional | ||
The number of decimal places to round to (default 0) | ||
round_method: rounding_method, optional | ||
The method by which to round each value. | ||
Can be one of { RoundingMethod.HALF_UP, RoundingMethod.HALF_EVEN } | ||
(default rounding_method.HALF_UP) | ||
Returns | ||
------- | ||
pylibcudf.Column | ||
A Column with values rounded | ||
""" | ||
cdef unique_ptr[column] c_result | ||
with nogil: | ||
c_result = move( | ||
cpp_round( | ||
source.view(), | ||
decimal_places, | ||
round_method | ||
) | ||
) | ||
|
||
return Column.from_libcudf(move(c_result)) |
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,38 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
import pyarrow as pa | ||
import pytest | ||
from utils import assert_column_eq | ||
|
||
import cudf._lib.pylibcudf as plc | ||
|
||
|
||
@pytest.fixture(params=[False, True]) | ||
def nullable(request): | ||
return request.param | ||
|
||
|
||
@pytest.fixture(params=["float32", "float64"]) | ||
def column(request, nullable): | ||
values = [2.5, 2.49, 1.6, 8, -1.5, -1.7, -0.5, 0.5] | ||
typ = {"float32": pa.float32(), "float64": pa.float64()}[request.param] | ||
if nullable: | ||
values[2] = None | ||
return plc.interop.from_arrow(pa.array(values, type=typ)) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"round_mode", ["half_towards_infinity", "half_to_even"] | ||
) | ||
@pytest.mark.parametrize("decimals", [0, 1, 2, 5]) | ||
def test_round(column, round_mode, decimals): | ||
method = { | ||
"half_towards_infinity": plc.round.RoundingMethod.HALF_UP, | ||
"half_to_even": plc.round.RoundingMethod.HALF_EVEN, | ||
}[round_mode] | ||
got = plc.round.round(column, decimals, method) | ||
expect = pa.compute.round( | ||
plc.interop.to_arrow(column), decimals, round_mode | ||
) | ||
|
||
assert_column_eq(expect, got) |