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

Allow numeric_only=True for reduction operations on numeric types #14111

Merged
merged 1 commit into from
Sep 14, 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: 4 additions & 2 deletions python/cudf/cudf/core/single_column_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ def _reduce(
if level is not None:
raise NotImplementedError("level parameter is not implemented yet")

if numeric_only:
if numeric_only and not isinstance(
self._column, cudf.core.column.numerical_base.NumericalBaseColumn
):
raise NotImplementedError(
f"Series.{op} does not implement numeric_only"
f"Series.{op} does not implement numeric_only."
)
try:
return getattr(self._column, op)(**kwargs)
Expand Down
44 changes: 22 additions & 22 deletions python/cudf/cudf/tests/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,30 +247,37 @@ def test_misc_quantiles(data, q):
],
)
@pytest.mark.parametrize("null_flag", [False, True])
def test_kurtosis_series(data, null_flag):
@pytest.mark.parametrize("numeric_only", [False, True])
def test_kurtosis_series(data, null_flag, numeric_only):
pdata = data.to_pandas()

if null_flag and len(data) > 2:
data.iloc[[0, 2]] = None
pdata.iloc[[0, 2]] = None

got = data.kurtosis()
got = data.kurtosis(numeric_only=numeric_only)
got = got if np.isscalar(got) else got.to_numpy()
expected = pdata.kurtosis()
expected = pdata.kurtosis(numeric_only=numeric_only)
np.testing.assert_array_almost_equal(got, expected)

got = data.kurt()
got = data.kurt(numeric_only=numeric_only)
got = got if np.isscalar(got) else got.to_numpy()
expected = pdata.kurt()
expected = pdata.kurt(numeric_only=numeric_only)
np.testing.assert_array_almost_equal(got, expected)

got = data.kurt(numeric_only=False)
got = got if np.isscalar(got) else got.to_numpy()
expected = pdata.kurt(numeric_only=False)
np.testing.assert_array_almost_equal(got, expected)

with pytest.raises(NotImplementedError):
data.kurt(numeric_only=True)
@pytest.mark.parametrize("op", ["skew", "kurt"])
def test_kurt_skew_error(op):
gs = cudf.Series(["ab", "cd"])
ps = gs.to_pandas()

with pytest.raises(FutureWarning):
assert_exceptions_equal(
getattr(gs, op),
getattr(ps, op),
lfunc_args_and_kwargs=([], {"numeric_only": True}),
rfunc_args_and_kwargs=([], {"numeric_only": True}),
)


@pytest.mark.parametrize(
Expand All @@ -290,26 +297,19 @@ def test_kurtosis_series(data, null_flag):
],
)
@pytest.mark.parametrize("null_flag", [False, True])
def test_skew_series(data, null_flag):
@pytest.mark.parametrize("numeric_only", [False, True])
def test_skew_series(data, null_flag, numeric_only):
pdata = data.to_pandas()

if null_flag and len(data) > 2:
data.iloc[[0, 2]] = None
pdata.iloc[[0, 2]] = None

got = data.skew()
expected = pdata.skew()
got = data.skew(numeric_only=numeric_only)
expected = pdata.skew(numeric_only=numeric_only)
got = got if np.isscalar(got) else got.to_numpy()
np.testing.assert_array_almost_equal(got, expected)

got = data.skew(numeric_only=False)
expected = pdata.skew(numeric_only=False)
got = got if np.isscalar(got) else got.to_numpy()
np.testing.assert_array_almost_equal(got, expected)

with pytest.raises(NotImplementedError):
data.skew(numeric_only=True)


@pytest.mark.parametrize("dtype", params_dtypes)
@pytest.mark.parametrize("num_na", [0, 1, 50, 99, 100])
Expand Down
Loading