Skip to content

Commit 6b575b4

Browse files
authored
CLN: Don't intercept NotImplementedError in _cython_transform (#49742)
1 parent 8020bf1 commit 6b575b4

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

pandas/core/groupby/generic.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -1357,13 +1357,9 @@ def arr_func(bvalues: ArrayLike) -> ArrayLike:
13571357

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

13691365
if len(res_mgr) < orig_mgr_len:

pandas/tests/groupby/test_function.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@ def test_cummin_cummax(self, df, method):
250250
def _check(self, df, method, expected_columns, expected_columns_numeric):
251251
gb = df.groupby("group")
252252

253+
# object dtypes for transformations are not implemented in Cython and
254+
# have no Python fallback
255+
exception = NotImplementedError if method.startswith("cum") else TypeError
256+
253257
if method in ("min", "max", "cummin", "cummax"):
254258
# The methods default to numeric_only=False and raise TypeError
255259
msg = "|".join(
@@ -258,7 +262,7 @@ def _check(self, df, method, expected_columns, expected_columns_numeric):
258262
"function is not implemented for this dtype",
259263
]
260264
)
261-
with pytest.raises(TypeError, match=msg):
265+
with pytest.raises(exception, match=msg):
262266
getattr(gb, method)()
263267
else:
264268
result = getattr(gb, method)()
@@ -274,7 +278,7 @@ def _check(self, df, method, expected_columns, expected_columns_numeric):
274278
"function is not implemented for this dtype",
275279
]
276280
)
277-
with pytest.raises(TypeError, match=msg):
281+
with pytest.raises(exception, match=msg):
278282
getattr(gb, method)(numeric_only=False)
279283
else:
280284
result = getattr(gb, method)(numeric_only=False)
@@ -1436,6 +1440,11 @@ def test_deprecate_numeric_only(
14361440
elif has_arg or kernel in ("idxmax", "idxmin"):
14371441
assert numeric_only is not True
14381442
# kernels that are successful on any dtype were above; this will fail
1443+
1444+
# object dtypes for transformations are not implemented in Cython and
1445+
# have no Python fallback
1446+
exception = NotImplementedError if kernel.startswith("cum") else TypeError
1447+
14391448
msg = "|".join(
14401449
[
14411450
"not allowed for this dtype",
@@ -1447,7 +1456,7 @@ def test_deprecate_numeric_only(
14471456
"function is not implemented for this dtype",
14481457
]
14491458
)
1450-
with pytest.raises(TypeError, match=msg):
1459+
with pytest.raises(exception, match=msg):
14511460
method(*args, **kwargs)
14521461
elif not has_arg and numeric_only is not lib.no_default:
14531462
with pytest.raises(

0 commit comments

Comments
 (0)