Skip to content

CLN: Don't intercept NotImplementedError in _cython_transform #49742

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

Merged
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
10 changes: 3 additions & 7 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1357,13 +1357,9 @@ def arr_func(bvalues: ArrayLike) -> ArrayLike:

# We could use `mgr.apply` here and not have to set_axis, but
# we would have to do shape gymnastics for ArrayManager compat
try:
res_mgr = mgr.grouped_reduce(
arr_func, ignore_failures=numeric_only is lib.no_default
)
except NotImplementedError as err:
# For NotImplementedError, args[0] is the error message
raise TypeError(err.args[0]) from err
res_mgr = mgr.grouped_reduce(
arr_func, ignore_failures=numeric_only is lib.no_default
)
res_mgr.set_axis(1, mgr.axes[1])

if len(res_mgr) < orig_mgr_len:
Expand Down
15 changes: 12 additions & 3 deletions pandas/tests/groupby/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ def test_cummin_cummax(self, df, method):
def _check(self, df, method, expected_columns, expected_columns_numeric):
gb = df.groupby("group")

# object dtypes for transformations are not implemented in Cython and
# have no Python fallback
exception = NotImplementedError if method.startswith("cum") else TypeError

if method in ("min", "max", "cummin", "cummax"):
# The methods default to numeric_only=False and raise TypeError
msg = "|".join(
Expand All @@ -258,7 +262,7 @@ def _check(self, df, method, expected_columns, expected_columns_numeric):
"function is not implemented for this dtype",
]
)
with pytest.raises(TypeError, match=msg):
with pytest.raises(exception, match=msg):
getattr(gb, method)()
else:
result = getattr(gb, method)()
Expand All @@ -274,7 +278,7 @@ def _check(self, df, method, expected_columns, expected_columns_numeric):
"function is not implemented for this dtype",
]
)
with pytest.raises(TypeError, match=msg):
with pytest.raises(exception, match=msg):
getattr(gb, method)(numeric_only=False)
else:
result = getattr(gb, method)(numeric_only=False)
Expand Down Expand Up @@ -1411,6 +1415,11 @@ def test_deprecate_numeric_only(
elif has_arg or kernel in ("idxmax", "idxmin"):
assert numeric_only is not True
# kernels that are successful on any dtype were above; this will fail

# object dtypes for transformations are not implemented in Cython and
# have no Python fallback
exception = NotImplementedError if kernel.startswith("cum") else TypeError

msg = "|".join(
[
"not allowed for this dtype",
Expand All @@ -1422,7 +1431,7 @@ def test_deprecate_numeric_only(
"function is not implemented for this dtype",
]
)
with pytest.raises(TypeError, match=msg):
with pytest.raises(exception, match=msg):
method(*args, **kwargs)
elif not has_arg and numeric_only is not lib.no_default:
with pytest.raises(
Expand Down