Skip to content

Commit

Permalink
Add support for __round__ in Series and DataFrame (#14099)
Browse files Browse the repository at this point in the history
Fixes: #14083 

This PR fixes builtin function `round` call on `DataFrame` & `Series` to work by implementing `__round__` method.

Authors:
  - GALI PREM SAGAR (https://github.com/galipremsagar)

Approvers:
  - Bradley Dice (https://github.com/bdice)

URL: #14099
  • Loading branch information
galipremsagar authored Sep 13, 2023
1 parent c13b783 commit 99c7711
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
6 changes: 6 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,12 @@ def _from_columns_like_self(
override_dtypes=override_dtypes,
)

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(
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)
29 changes: 29 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,31 @@ 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)

# 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)

assert_eq(expected, actual)

0 comments on commit 99c7711

Please sign in to comment.