Skip to content

Commit 46d88c1

Browse files
jbrockmendelWillAyd
authored andcommitted
CLN: Exception*2 in groupby wrapper (#28771)
1 parent 39602e7 commit 46d88c1

File tree

1 file changed

+32
-25
lines changed

1 file changed

+32
-25
lines changed

pandas/core/groupby/groupby.py

+32-25
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class providing the base-class of operations.
1111
from contextlib import contextmanager
1212
import datetime
1313
from functools import partial, wraps
14+
import inspect
15+
import re
1416
import types
1517
from typing import FrozenSet, List, Optional, Tuple, Type, Union
1618

@@ -613,48 +615,53 @@ def _make_wrapper(self, name):
613615
return self.apply(lambda self: getattr(self, name))
614616

615617
f = getattr(type(self._selected_obj), name)
618+
sig = inspect.signature(f)
616619

617620
def wrapper(*args, **kwargs):
618621
# a little trickery for aggregation functions that need an axis
619622
# argument
620-
kwargs_with_axis = kwargs.copy()
621-
if "axis" not in kwargs_with_axis or kwargs_with_axis["axis"] is None:
622-
kwargs_with_axis["axis"] = self.axis
623-
624-
def curried_with_axis(x):
625-
return f(x, *args, **kwargs_with_axis)
623+
if "axis" in sig.parameters:
624+
if kwargs.get("axis", None) is None:
625+
kwargs["axis"] = self.axis
626626

627627
def curried(x):
628628
return f(x, *args, **kwargs)
629629

630630
# preserve the name so we can detect it when calling plot methods,
631631
# to avoid duplicates
632-
curried.__name__ = curried_with_axis.__name__ = name
632+
curried.__name__ = name
633633

634634
# special case otherwise extra plots are created when catching the
635635
# exception below
636636
if name in base.plotting_methods:
637637
return self.apply(curried)
638638

639639
try:
640-
return self.apply(curried_with_axis)
641-
except Exception:
642-
try:
643-
return self.apply(curried)
644-
except Exception:
645-
646-
# related to : GH3688
647-
# try item-by-item
648-
# this can be called recursively, so need to raise
649-
# ValueError
650-
# if we don't have this method to indicated to aggregate to
651-
# mark this column as an error
652-
try:
653-
return self._aggregate_item_by_item(name, *args, **kwargs)
654-
except AttributeError:
655-
# e.g. SparseArray has no flags attr
656-
raise ValueError
657-
640+
return self.apply(curried)
641+
except TypeError as err:
642+
if not re.search(
643+
"reduction operation '.*' not allowed for this dtype", str(err)
644+
):
645+
# We don't have a cython implementation
646+
# TODO: is the above comment accurate?
647+
raise
648+
649+
# related to : GH3688
650+
# try item-by-item
651+
# this can be called recursively, so need to raise
652+
# ValueError
653+
# if we don't have this method to indicated to aggregate to
654+
# mark this column as an error
655+
try:
656+
return self._aggregate_item_by_item(name, *args, **kwargs)
657+
except AttributeError:
658+
# e.g. SparseArray has no flags attr
659+
# FIXME: 'SeriesGroupBy' has no attribute '_aggregate_item_by_item'
660+
# occurs in idxmax() case
661+
# in tests.groupby.test_function.test_non_cython_api
662+
raise ValueError
663+
664+
wrapper.__name__ = name
658665
return wrapper
659666

660667
def get_group(self, name, obj=None):

0 commit comments

Comments
 (0)