Skip to content

Commit

Permalink
Implement __round__ to classes only where they are needed
Browse files Browse the repository at this point in the history
  • Loading branch information
galipremsagar committed Sep 13, 2023
1 parent 258e0fe commit 2511f8c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
3 changes: 3 additions & 0 deletions python/cudf/cudf/core/indexed_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
23 changes: 23 additions & 0 deletions python/cudf/cudf/tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
27 changes: 27 additions & 0 deletions python/cudf/cudf/tests/test_series.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) 2020-2023, NVIDIA CORPORATION.

import decimal
import hashlib
import operator
import re
Expand Down Expand Up @@ -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)

0 comments on commit 2511f8c

Please sign in to comment.