diff --git a/python/cudf/cudf/core/indexed_frame.py b/python/cudf/cudf/core/indexed_frame.py index 95f3d4d01d5..0e6a5e03ea6 100644 --- a/python/cudf/cudf/core/indexed_frame.py +++ b/python/cudf/cudf/core/indexed_frame.py @@ -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 ) diff --git a/python/cudf/cudf/tests/test_series.py b/python/cudf/cudf/tests/test_series.py index 99bd9adb034..f8697c5c6b8 100644 --- a/python/cudf/cudf/tests/test_series.py +++ b/python/cudf/cudf/tests/test_series.py @@ -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):