From 4d73f1144f42279594212131a69b18b00efa09d2 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sun, 13 Oct 2019 17:01:35 -0700 Subject: [PATCH 1/3] catch less --- pandas/core/groupby/generic.py | 13 +++++++++---- pandas/core/groupby/groupby.py | 12 +++--------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index 7be11696b7d45..ee765073e5c93 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -952,6 +952,7 @@ def _cython_agg_blocks(self, how, alt=None, numeric_only=True, min_count=-1): if alt is None: # we cannot perform the operation # in an alternate way, exclude the block + assert how == "ohlc" deleted_items.append(locs) continue @@ -1025,17 +1026,20 @@ def _aggregate_frame(self, func, *args, **kwargs): if axis != obj._info_axis_number: try: for name, data in self: - result[name] = self._try_cast(func(data, *args, **kwargs), data) + fres = func(data, *args, **kwargs) + result[name] = self._try_cast(fres, data) except Exception: return self._aggregate_item_by_item(func, *args, **kwargs) else: for name in self.indices: + data = self.get_group(name, obj=obj) try: - data = self.get_group(name, obj=obj) - result[name] = self._try_cast(func(data, *args, **kwargs), data) + fres = func(data, *args, **kwargs) except Exception: wrapper = lambda x: func(x, *args, **kwargs) result[name] = data.apply(wrapper, axis=axis) + else: + result[name] = self._try_cast(fres, data) return self._wrap_frame_output(result, obj) @@ -1410,9 +1414,10 @@ def _transform_item_by_item(self, obj, wrapper): for i, col in enumerate(obj): try: output[col] = self[col].transform(wrapper) - inds.append(i) except Exception: pass + else: + inds.append(i) if len(output) == 0: raise TypeError("Transform function invalid for data types") diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index cc297629a7004..8461b4381e2ea 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -598,14 +598,7 @@ def pipe(self, func, *args, **kwargs): plot = property(GroupByPlot) def _make_wrapper(self, name): - if name not in self._apply_whitelist: - is_callable = callable(getattr(self._selected_obj, name, None)) - kind = " callable " if is_callable else " " - msg = ( - "Cannot access{0}attribute {1!r} of {2!r} objects, try " - "using the 'apply' method".format(kind, name, type(self).__name__) - ) - raise AttributeError(msg) + assert name in self._apply_whitelist self._set_group_selection() @@ -919,9 +912,10 @@ def _python_agg_general(self, func, *args, **kwargs): for name, obj in self._iterate_slices(): try: result, counts = self.grouper.agg_series(obj, f) - output[name] = self._try_cast(result, obj, numeric_only=True) except TypeError: continue + else: + output[name] = self._try_cast(result, obj, numeric_only=True) if len(output) == 0: return self._python_apply_general(f) From ea0730139649807ed58641a0900af8649c8e8c3e Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sun, 13 Oct 2019 19:07:33 -0700 Subject: [PATCH 2/3] cln --- pandas/_libs/algos_take_helper.pxi.in | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/_libs/algos_take_helper.pxi.in b/pandas/_libs/algos_take_helper.pxi.in index 3a3adc71875ed..f10061a417c03 100644 --- a/pandas/_libs/algos_take_helper.pxi.in +++ b/pandas/_libs/algos_take_helper.pxi.in @@ -276,7 +276,6 @@ cdef _take_2d(ndarray[take_t, ndim=2] values, object idx): Py_ssize_t i, j, N, K ndarray[Py_ssize_t, ndim=2, cast=True] indexer = idx ndarray[take_t, ndim=2] result - object val N, K = (values).shape From ea65439292aca29f4035761899e037cd804fcad9 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sun, 13 Oct 2019 19:12:34 -0700 Subject: [PATCH 3/3] CLN: catch less --- pandas/core/base.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index 56ffd3db6e942..d07a120560196 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -267,7 +267,7 @@ def aggregate(self, func, *args, **kwargs): agg = aggregate - def _try_aggregate_string_function(self, arg, *args, **kwargs): + def _try_aggregate_string_function(self, arg: str, *args, **kwargs): """ if arg is a string, then try to operate on it: - try to find a function (or attribute) on ourselves @@ -292,12 +292,10 @@ def _try_aggregate_string_function(self, arg, *args, **kwargs): f = getattr(np, arg, None) if f is not None: - try: + if hasattr(self, "__array__"): + # in particular exclude Window return f(self, *args, **kwargs) - except (AttributeError, TypeError): - pass - raise AttributeError( "'{arg}' is not a valid function for " "'{cls}' object".format(arg=arg, cls=type(self).__name__)