Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for __round__ in Series and DataFrame #14099

Merged
merged 4 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
galipremsagar marked this conversation as resolved.
Show resolved Hide resolved
# 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)