From 23d8552e7d37230e0a7b4664ae23a0ea9d5ee1ad Mon Sep 17 00:00:00 2001 From: galipremsagar Date: Wed, 1 Feb 2023 10:02:43 -0800 Subject: [PATCH 1/2] fix round API --- python/cudf/cudf/core/indexed_frame.py | 6 +++- python/cudf/cudf/tests/test_dataframe.py | 39 ++++++++++++------------ 2 files changed, 24 insertions(+), 21 deletions(-) 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..4b39fd71420 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_will_nan", "floats_same"], + ), + cudf.Series( + [-4, -2, 12], index=["ints", "floats_will_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) From d8f3a3f44d0dc315a70c404ff369112f46b4e936 Mon Sep 17 00:00:00 2001 From: GALI PREM SAGAR Date: Thu, 2 Feb 2023 09:44:11 -0600 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: brandon-b-miller <53796099+brandon-b-miller@users.noreply.github.com> --- python/cudf/cudf/tests/test_dataframe.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/cudf/cudf/tests/test_dataframe.py b/python/cudf/cudf/tests/test_dataframe.py index 4b39fd71420..1191a30f718 100644 --- a/python/cudf/cudf/tests/test_dataframe.py +++ b/python/cudf/cudf/tests/test_dataframe.py @@ -3815,10 +3815,10 @@ def test_ndim(): 5, pd.Series( [1, 4, 3, -6], - index=["floats", "ints", "floats_will_nan", "floats_same"], + index=["floats", "ints", "floats_with_nan", "floats_same"], ), cudf.Series( - [-4, -2, 12], index=["ints", "floats_will_nan", "floats_same"] + [-4, -2, 12], index=["ints", "floats_with_nan", "floats_same"] ), {"floats": -1, "ints": 15, "floats_will_nan": 2}, ],