Skip to content

Commit 4448af8

Browse files
authored
Add pylibcudf interface to cudf::round_decimal (rapidsai#21332)
Adds a pylibcudf interface for `cudf::round_decimal` and deprecates the pylibcudf interface to `cudf::round`. Reference rapidsai#21319 Authors: - David Wendt (https://github.com/davidwendt) Approvers: - Bradley Dice (https://github.com/bdice) URL: rapidsai#21332
1 parent 45a13cc commit 4448af8

4 files changed

Lines changed: 84 additions & 5 deletions

File tree

python/pylibcudf/pylibcudf/libcudf/round.pxd

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-FileCopyrightText: Copyright (c) 2021-2025, NVIDIA CORPORATION.
1+
# SPDX-FileCopyrightText: Copyright (c) 2021-2026, NVIDIA CORPORATION.
22
# SPDX-License-Identifier: Apache-2.0
33
from libc.stdint cimport int32_t
44
from libcpp.memory cimport unique_ptr
@@ -23,3 +23,11 @@ cdef extern from "cudf/round.hpp" namespace "cudf" nogil:
2323
cuda_stream_view stream,
2424
device_memory_resource* mr
2525
) except +libcudf_exception_handler
26+
27+
cdef unique_ptr[column] round_decimal (
28+
const column_view& input,
29+
int32_t decimal_places,
30+
rounding_method method,
31+
cuda_stream_view stream,
32+
device_memory_resource* mr
33+
) except +libcudf_exception_handler

python/pylibcudf/pylibcudf/round.pxd

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-FileCopyrightText: Copyright (c) 2024-2025, NVIDIA CORPORATION.
1+
# SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION.
22
# SPDX-License-Identifier: Apache-2.0
33
from libc.stdint cimport int32_t
44
from pylibcudf.libcudf.round cimport rounding_method
@@ -16,3 +16,11 @@ cpdef Column round(
1616
Stream stream = *,
1717
DeviceMemoryResource mr = *
1818
)
19+
20+
cpdef Column round_decimal(
21+
Column source,
22+
int32_t decimal_places = *,
23+
rounding_method round_method = *,
24+
Stream stream = *,
25+
DeviceMemoryResource mr = *
26+
)

python/pylibcudf/pylibcudf/round.pyi

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-FileCopyrightText: Copyright (c) 2024, NVIDIA CORPORATION.
1+
# SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION.
22
# SPDX-License-Identifier: Apache-2.0
33

44
from enum import IntEnum
@@ -19,3 +19,10 @@ def round(
1919
stream: Stream | None = None,
2020
mr: DeviceMemoryResource | None = None,
2121
) -> Column: ...
22+
def round_decimal(
23+
source: Column,
24+
decimal_places: int = 0,
25+
round_method: RoundingMethod = RoundingMethod.HALF_UP,
26+
stream: Stream | None = None,
27+
mr: DeviceMemoryResource | None = None,
28+
) -> Column: ...

python/pylibcudf/pylibcudf/round.pyx

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
# SPDX-FileCopyrightText: Copyright (c) 2024-2025, NVIDIA CORPORATION.
1+
# SPDX-FileCopyrightText: Copyright (c) 2024-2026, NVIDIA CORPORATION.
22
# SPDX-License-Identifier: Apache-2.0
33
from libc.stdint cimport int32_t
44
from libcpp.memory cimport unique_ptr
55
from libcpp.utility cimport move
6-
from pylibcudf.libcudf.round cimport round as cpp_round, rounding_method
6+
from pylibcudf.libcudf.round cimport (
7+
round as cpp_round,
8+
round_decimal as cpp_round_decimal,
9+
rounding_method,
10+
)
711

812
from pylibcudf.libcudf.round import \
913
rounding_method as RoundingMethod # no-cython-lint
@@ -27,6 +31,10 @@ cpdef Column round(
2731
):
2832
"""Rounds all the values in a column to the specified number of decimal places.
2933
34+
.. deprecated:: release 26.04
35+
round is deprecated for float type.
36+
Use ``round_decimal`` for decimal and integer types.
37+
3038
For details, see :cpp:func:`round`.
3139
3240
Parameters
@@ -64,4 +72,52 @@ cpdef Column round(
6472

6573
return Column.from_libcudf(move(c_result), stream, mr)
6674

75+
76+
cpdef Column round_decimal(
77+
Column source,
78+
int32_t decimal_places = 0,
79+
rounding_method round_method = rounding_method.HALF_UP,
80+
Stream stream=None,
81+
DeviceMemoryResource mr=None
82+
):
83+
"""Rounds all the values in a column to the specified number of decimal places.
84+
Only decimal and integer types are supported.
85+
86+
For details, see :cpp:func:`round_decimal`.
87+
88+
Parameters
89+
----------
90+
source : Column
91+
The Column for which to round values.
92+
decimal_places: int32_t, optional
93+
The number of decimal places to round to (default 0)
94+
round_method: rounding_method, optional
95+
The method by which to round each value.
96+
Can be one of { RoundingMethod.HALF_UP, RoundingMethod.HALF_EVEN }
97+
(default rounding_method.HALF_UP)
98+
stream : Stream | None
99+
CUDA stream on which to perform the operation.
100+
mr : DeviceMemoryResource | None
101+
Device memory resource used to allocate the returned column's device memory.
102+
103+
Returns
104+
-------
105+
pylibcudf.Column
106+
A Column with values rounded
107+
"""
108+
cdef unique_ptr[column] c_result
109+
stream = _get_stream(stream)
110+
mr = _get_memory_resource(mr)
111+
112+
with nogil:
113+
c_result = cpp_round_decimal(
114+
source.view(),
115+
decimal_places,
116+
round_method,
117+
stream.view(),
118+
mr.get_mr()
119+
)
120+
121+
return Column.from_libcudf(move(c_result), stream, mr)
122+
67123
RoundingMethod.__str__ = RoundingMethod.__repr__

0 commit comments

Comments
 (0)