Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable rounding for Decimal32 and Decimal64 in cuDF #17332

Merged
merged 19 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/cudf/cudf/core/indexed_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3936,7 +3936,7 @@ def round(self, decimals=0, how="half_even"):

cols = (
col.round(decimals[name], how=how)
if name in decimals and col.dtype.kind in "fiu"
if name in decimals and (col.dtype.kind in "fiu" or isinstance(col.dtype, (cudf.Decimal32Dtype, cudf.Decimal64Dtype)))
a-hirota marked this conversation as resolved.
Show resolved Hide resolved
else col.copy(deep=True)
for name, col in self._column_labels_and_values
)
Expand Down
37 changes: 37 additions & 0 deletions python/cudf/cudf/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import pytest

import cudf
from cudf.core.dtypes import Decimal32Dtype
from cudf.core.dtypes import Decimal64Dtype

a-hirota marked this conversation as resolved.
Show resolved Hide resolved
from cudf.api.extensions import no_default
from cudf.core._compat import PANDAS_CURRENT_SUPPORTED_VERSION, PANDAS_VERSION
from cudf.errors import MixedTypeError
Expand Down Expand Up @@ -772,6 +775,40 @@ def test_round_nan_as_null_false(series, decimal):
assert_eq(result, expected, atol=1e-10)


@pytest.mark.parametrize(
"data, dtype, decimals, expected_half_up, expected_half_even",
[
# Decimal32Dtype Test Cases
([1.234, 2.345, 3.456], Decimal32Dtype(precision=5, scale=3), 2, [1.23, 2.35, 3.46], [1.23, 2.34, 3.46]),
([1.234, 2.345, 3.456], Decimal32Dtype(precision=5, scale=3), 0, [1.0, 2.0, 3.0], [1.0, 2.0, 3.0]),
([1.234, 2.345, 3.456], Decimal32Dtype(precision=5, scale=3), 3, [1.234, 2.345, 3.456], [1.234, 2.345, 3.456]),
# Decimal64Dtype Test Cases
([1.234567, 2.345678, 3.456789], Decimal64Dtype(precision=10, scale=6), 4, [1.2346, 2.3457, 3.4568], [1.2346, 2.3457, 3.4568]),
([1.234567, 2.345678, 3.456789], Decimal64Dtype(precision=10, scale=6), 2, [1.23, 2.35, 3.46], [1.23, 2.35, 3.46]),
([1.234567, 2.345678, 3.456789], Decimal64Dtype(precision=10, scale=6), 6, [1.234567, 2.345678, 3.456789], [1.234567, 2.345678, 3.456789]),
a-hirota marked this conversation as resolved.
Show resolved Hide resolved
],
)
def test_series_round_decimal(data, dtype, decimals, expected_half_up, expected_half_even):
ser = cudf.Series(data).astype(dtype)

# Half-up rounding
result_half_up = ser.round(decimals=decimals, how="half_up").astype(dtype)
a-hirota marked this conversation as resolved.
Show resolved Hide resolved
# print("Rounded Series (half_up):", result_half_up)

expected_ser_half_up = cudf.Series(expected_half_up).astype(dtype)
# print("Expected Series (half_up):", expected_ser_half_up)

assert_eq(result_half_up, expected_ser_half_up)

# Half-even rounding
result_half_even = ser.round(decimals=decimals, how="half_even").astype(dtype)
# print("Rounded Series (half_even):", result_half_even)

expected_ser_half_even = cudf.Series(expected_half_even).astype(dtype)
# print("Expected Series (half_even):", expected_ser_half_even)
assert_eq(result_half_even, expected_ser_half_even)


@pytest.mark.parametrize("ps", _series_na_data())
@pytest.mark.parametrize("nan_as_null", [True, False, None])
def test_series_isnull_isna(ps, nan_as_null):
Expand Down
Loading