diff --git a/python/cudf/cudf/core/indexed_frame.py b/python/cudf/cudf/core/indexed_frame.py index daf2502ee96..25c89c34040 100644 --- a/python/cudf/cudf/core/indexed_frame.py +++ b/python/cudf/cudf/core/indexed_frame.py @@ -2496,7 +2496,11 @@ def round(self, decimals=0, how="half_even"): cols = { name: col.round(decimals[name], how=how) - if (name in decimals and _is_non_decimal_numeric_dtype(col.dtype)) + if ( + name in decimals + and _is_non_decimal_numeric_dtype(col.dtype) + and not is_bool_dtype(col.dtype) + ) else col.copy(deep=True) for name, col in self._data.items() } diff --git a/python/cudf/cudf/tests/test_dataframe.py b/python/cudf/cudf/tests/test_dataframe.py index 65e24c7c704..1191a30f718 100644 --- a/python/cudf/cudf/tests/test_dataframe.py +++ b/python/cudf/cudf/tests/test_dataframe.py @@ -3813,17 +3813,22 @@ def test_ndim(): -3, 0, 5, - pd.Series([1, 4, 3, -6], index=["w", "x", "y", "z"]), - cudf.Series([-4, -2, 12], index=["x", "y", "z"]), - {"w": -1, "x": 15, "y": 2}, + pd.Series( + [1, 4, 3, -6], + index=["floats", "ints", "floats_with_nan", "floats_same"], + ), + cudf.Series( + [-4, -2, 12], index=["ints", "floats_with_nan", "floats_same"] + ), + {"floats": -1, "ints": 15, "floats_will_nan": 2}, ], ) def test_dataframe_round(decimals): - pdf = pd.DataFrame( + gdf = cudf.DataFrame( { - "w": np.arange(0.5, 10.5, 1), - "x": np.random.normal(-100, 100, 10), - "y": np.array( + "floats": np.arange(0.5, 10.5, 1), + "ints": np.random.normal(-100, 100, 10), + "floats_with_na": np.array( [ 14.123, 2.343, @@ -3832,31 +3837,25 @@ def test_dataframe_round(decimals): -8.302, np.nan, 94.313, - -112.236, + None, -8.029, np.nan, ] ), - "z": np.repeat([-0.6459412758761901], 10), + "floats_same": np.repeat([-0.6459412758761901], 10), + "bools": np.random.choice([True, None, False], 10), + "strings": np.random.choice(["abc", "xyz", None], 10), + "struct": np.random.choice([{"abc": 1}, {"xyz": 2}, None], 10), + "list": [[1], [2], None, [4], [3]] * 2, } ) - gdf = cudf.DataFrame.from_pandas(pdf) + pdf = gdf.to_pandas() if isinstance(decimals, cudf.Series): pdecimals = decimals.to_pandas() else: pdecimals = decimals - result = gdf.round(decimals) - expected = pdf.round(pdecimals) - assert_eq(result, expected) - - # with nulls, maintaining existing null mask - for c in pdf.columns: - arr = pdf[c].to_numpy().astype("float64") # for pandas nulls - arr.ravel()[np.random.choice(10, 5, replace=False)] = np.nan - pdf[c] = gdf[c] = arr - result = gdf.round(decimals) expected = pdf.round(pdecimals)