diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index cb56f7b8d535b..61a04431f99cb 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -11,6 +11,8 @@ class providing the base-class of operations. from contextlib import contextmanager import datetime from functools import partial, wraps +import inspect +import re import types from typing import FrozenSet, List, Optional, Tuple, Type, Union @@ -613,23 +615,21 @@ def _make_wrapper(self, name): return self.apply(lambda self: getattr(self, name)) f = getattr(type(self._selected_obj), name) + sig = inspect.signature(f) def wrapper(*args, **kwargs): # a little trickery for aggregation functions that need an axis # argument - kwargs_with_axis = kwargs.copy() - if "axis" not in kwargs_with_axis or kwargs_with_axis["axis"] is None: - kwargs_with_axis["axis"] = self.axis - - def curried_with_axis(x): - return f(x, *args, **kwargs_with_axis) + if "axis" in sig.parameters: + if kwargs.get("axis", None) is None: + kwargs["axis"] = self.axis def curried(x): return f(x, *args, **kwargs) # preserve the name so we can detect it when calling plot methods, # to avoid duplicates - curried.__name__ = curried_with_axis.__name__ = name + curried.__name__ = name # special case otherwise extra plots are created when catching the # exception below @@ -637,24 +637,31 @@ def curried(x): return self.apply(curried) try: - return self.apply(curried_with_axis) - except Exception: - try: - return self.apply(curried) - except Exception: - - # related to : GH3688 - # try item-by-item - # this can be called recursively, so need to raise - # ValueError - # if we don't have this method to indicated to aggregate to - # mark this column as an error - try: - return self._aggregate_item_by_item(name, *args, **kwargs) - except AttributeError: - # e.g. SparseArray has no flags attr - raise ValueError - + return self.apply(curried) + except TypeError as err: + if not re.search( + "reduction operation '.*' not allowed for this dtype", str(err) + ): + # We don't have a cython implementation + # TODO: is the above comment accurate? + raise + + # related to : GH3688 + # try item-by-item + # this can be called recursively, so need to raise + # ValueError + # if we don't have this method to indicated to aggregate to + # mark this column as an error + try: + return self._aggregate_item_by_item(name, *args, **kwargs) + except AttributeError: + # e.g. SparseArray has no flags attr + # FIXME: 'SeriesGroupBy' has no attribute '_aggregate_item_by_item' + # occurs in idxmax() case + # in tests.groupby.test_function.test_non_cython_api + raise ValueError + + wrapper.__name__ = name return wrapper def get_group(self, name, obj=None):