From 2511f8c4231af7a4a214a9b9ecbef68841943bd4 Mon Sep 17 00:00:00 2001 From: galipremsagar Date: Tue, 12 Sep 2023 17:36:36 -0700 Subject: [PATCH 1/3] Implement __round__ to classes only where they are needed --- python/cudf/cudf/core/indexed_frame.py | 3 +++ python/cudf/cudf/tests/test_dataframe.py | 23 ++++++++++++++++++++ python/cudf/cudf/tests/test_series.py | 27 ++++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/python/cudf/cudf/core/indexed_frame.py b/python/cudf/cudf/core/indexed_frame.py index 69b25c51a66..7b0253de3bf 100644 --- a/python/cudf/cudf/core/indexed_frame.py +++ b/python/cudf/cudf/core/indexed_frame.py @@ -358,6 +358,9 @@ def _from_columns_like_self( override_dtypes=override_dtypes, ) + def __round__(self, digits=0): + return self.round(decimals=digits) + def _mimic_inplace( self, result: Self, inplace: bool = False ) -> Optional[Self]: diff --git a/python/cudf/cudf/tests/test_dataframe.py b/python/cudf/cudf/tests/test_dataframe.py index 44d0b9249d0..61372bab3ad 100644 --- a/python/cudf/cudf/tests/test_dataframe.py +++ b/python/cudf/cudf/tests/test_dataframe.py @@ -10326,3 +10326,26 @@ def test_dataframe_nlargest_nsmallest_str_error(attr): ([], {"n": 1, "columns": ["a", "b"]}), ([], {"n": 1, "columns": ["a", "b"]}), ) + + +@pytest.mark.parametrize("digits", [0, 1, 3, 4, 10]) +def test_dataframe_round_builtin(digits): + pdf = pd.DataFrame( + { + "a": [1.2234242333234, 323432.3243423, np.nan], + "b": ["a", "b", "c"], + "c": pd.Series([34224, 324324, 324342], dtype="datetime64[ns]"), + "d": pd.Series([224.242, None, 2424.234324], dtype="category"), + "e": [ + decimal.Decimal("342.3243234234242"), + decimal.Decimal("89.32432497687622"), + None, + ], + } + ) + gdf = cudf.from_pandas(pdf, nan_as_null=False) + + expected = round(pdf, digits) + actual = round(gdf, digits) + + assert_eq(expected, actual) diff --git a/python/cudf/cudf/tests/test_series.py b/python/cudf/cudf/tests/test_series.py index 8a652caa6e2..ef2d76f818f 100644 --- a/python/cudf/cudf/tests/test_series.py +++ b/python/cudf/cudf/tests/test_series.py @@ -1,5 +1,6 @@ # Copyright (c) 2020-2023, NVIDIA CORPORATION. +import decimal import hashlib import operator import re @@ -2282,3 +2283,29 @@ def test_series_rename(initial_name, name): expected = psr.rename(name) assert_eq(actual, expected) + + +@pytest.mark.parametrize( + "data", + [ + [1.2234242333234, 323432.3243423, np.nan], + pd.Series([34224, 324324, 324342], dtype="datetime64[ns]"), + pd.Series([224.242, None, 2424.234324], dtype="category"), + [ + decimal.Decimal("342.3243234234242"), + decimal.Decimal("89.32432497687622"), + None, + ], + ], +) +@pytest.mark.parametrize("digits", [0, 1, 3, 4, 10]) +def test_series_round_builtin(data, digits): + ps = pd.Series(data) + gs = cudf.from_pandas(ps, nan_as_null=False) + + # round(Series) seems buggy. + expected = round(ps.to_frame(), digits)[0] + expected.name = None + actual = round(gs, digits) + + assert_eq(expected, actual) From 681f1223b01dd49bd43aae01f5ae86cb28928eeb Mon Sep 17 00:00:00 2001 From: galipremsagar Date: Tue, 12 Sep 2023 17:48:01 -0700 Subject: [PATCH 2/3] document bug --- python/cudf/cudf/tests/test_series.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/python/cudf/cudf/tests/test_series.py b/python/cudf/cudf/tests/test_series.py index ef2d76f818f..798809b0ada 100644 --- a/python/cudf/cudf/tests/test_series.py +++ b/python/cudf/cudf/tests/test_series.py @@ -2303,7 +2303,9 @@ def test_series_round_builtin(data, digits): ps = pd.Series(data) gs = cudf.from_pandas(ps, nan_as_null=False) - # round(Series) seems buggy. + # TODO: Remove `to_frame` workaround + # after following issue is fixed: + # https://github.com/pandas-dev/pandas/issues/55114 expected = round(ps.to_frame(), digits)[0] expected.name = None actual = round(gs, digits) From 81194fb809159103768a914b5c23072d44180f5a Mon Sep 17 00:00:00 2001 From: GALI PREM SAGAR Date: Tue, 12 Sep 2023 20:10:12 -0500 Subject: [PATCH 3/3] add comment --- python/cudf/cudf/core/indexed_frame.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/python/cudf/cudf/core/indexed_frame.py b/python/cudf/cudf/core/indexed_frame.py index 7b0253de3bf..f36bec1b94e 100644 --- a/python/cudf/cudf/core/indexed_frame.py +++ b/python/cudf/cudf/core/indexed_frame.py @@ -359,6 +359,9 @@ def _from_columns_like_self( ) def __round__(self, digits=0): + # Shouldn't be added to BinaryOperand + # because pandas Index doesn't implement + # this method. return self.round(decimals=digits) def _mimic_inplace(