Skip to content

Commit

Permalink
Enable rounding for Decimal32 and Decimal64 in cuDF (#17332)
Browse files Browse the repository at this point in the history
Authors:
  - Hirota Akio (https://github.com/a-hirota)
  - Vyas Ramasubramani (https://github.com/vyasr)

Approvers:
  - Matthew Roeschke (https://github.com/mroeschke)

URL: #17332
  • Loading branch information
a-hirota authored Dec 10, 2024
1 parent ebad043 commit 4764395
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
8 changes: 7 additions & 1 deletion python/cudf/cudf/core/indexed_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3970,7 +3970,13 @@ 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)
)
)
else col.copy(deep=True)
for name, col in self._column_labels_and_values
)
Expand Down
63 changes: 63 additions & 0 deletions python/cudf/cudf/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,69 @@ 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",
[
(
[1.234, 2.345, 3.456],
cudf.Decimal32Dtype(precision=5, scale=3),
2,
[1.23, 2.35, 3.46],
[1.23, 2.34, 3.46],
),
(
[1.234, 2.345, 3.456],
cudf.Decimal32Dtype(precision=5, scale=3),
0,
[1.0, 2.0, 3.0],
[1.0, 2.0, 3.0],
),
(
[1.234, 2.345, 3.456],
cudf.Decimal32Dtype(precision=5, scale=3),
3,
[1.234, 2.345, 3.456],
[1.234, 2.345, 3.456],
),
(
[1.234567, 2.345678, 3.456789],
cudf.Decimal64Dtype(precision=10, scale=6),
4,
[1.2346, 2.3457, 3.4568],
[1.2346, 2.3457, 3.4568],
),
(
[1.234567, 2.345678, 3.456789],
cudf.Decimal64Dtype(precision=10, scale=6),
2,
[1.23, 2.35, 3.46],
[1.23, 2.35, 3.46],
),
(
[1.234567, 2.345678, 3.456789],
cudf.Decimal64Dtype(precision=10, scale=6),
6,
[1.234567, 2.345678, 3.456789],
[1.234567, 2.345678, 3.456789],
),
],
)
def test_series_round_decimal(
data, dtype, decimals, expected_half_up, expected_half_even
):
ser = cudf.Series(data).astype(dtype)

result_half_up = ser.round(decimals=decimals, how="half_up").astype(dtype)
expected_ser_half_up = cudf.Series(expected_half_up).astype(dtype)
assert_eq(result_half_up, expected_ser_half_up)

result_half_even = ser.round(decimals=decimals, how="half_even").astype(
dtype
)
expected_ser_half_even = cudf.Series(expected_half_even).astype(dtype)
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

0 comments on commit 4764395

Please sign in to comment.