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

[REVIEW] Handle bool types in round API #12670

Merged
merged 2 commits into from
Feb 2, 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: 5 additions & 1 deletion python/cudf/cudf/core/indexed_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
39 changes: 19 additions & 20 deletions python/cudf/cudf/tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_with_nan", "floats_same"],
),
cudf.Series(
[-4, -2, 12], index=["ints", "floats_with_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,
Expand All @@ -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)

Expand Down