From e01b57adac056901c63b2dd74a95fbe488c1df89 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 23 Oct 2019 14:25:20 -0700 Subject: [PATCH] CLN: AttributeError in _wrap_applied_output --- pandas/core/base.py | 4 ---- pandas/core/groupby/generic.py | 19 ++++++++++++++----- pandas/core/groupby/ops.py | 10 ++++++++-- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index 5ae3926952a67..9586d49c555ff 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -571,8 +571,6 @@ def _aggregate_multiple_funcs(self, arg, _level, _axis): except (TypeError, DataError): pass - except SpecificationError: - raise else: results.append(new_res) @@ -591,8 +589,6 @@ def _aggregate_multiple_funcs(self, arg, _level, _axis): except ValueError: # cannot aggregate continue - except SpecificationError: - raise else: results.append(new_res) keys.append(col) diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index c766fcaa4f849..58ede6307525c 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -475,7 +475,7 @@ def _transform_fast(self, func, func_nm): out = self._try_cast(out, self.obj) return Series(out, index=self.obj.index, name=self.obj.name) - def filter(self, func, dropna=True, *args, **kwargs): # noqa + def filter(self, func, dropna=True, *args, **kwargs): """ Return a copy of a Series excluding elements from groups that do not satisfy the boolean criterion specified by func. @@ -1230,7 +1230,7 @@ def first_not_none(values): return self._concat_objects(keys, values, not_indexed_same=True) try: - if self.axis == 0: + if self.axis == 0 and isinstance(v, ABCSeries): # GH6124 if the list of Series have a consistent name, # then propagate that name to the result. index = v.index.copy() @@ -1266,15 +1266,24 @@ def first_not_none(values): axis=self.axis, ).unstack() result.columns = index - else: + elif isinstance(v, ABCSeries): stacked_values = np.vstack([np.asarray(v) for v in values]) result = DataFrame( stacked_values.T, index=v.index, columns=key_index ) + else: + # GH#1738: values is list of arrays of unequal lengths + # fall through to the outer else clause + # TODO: sure this is right? we used to do this + # after raising AttributeError above + return Series( + values, index=key_index, name=self._selection_name + ) - except (ValueError, AttributeError): + except ValueError: + # TODO: not reached in tests; is this still needed? # GH1738: values is list of arrays of unequal lengths fall - # through to the outer else caluse + # through to the outer else clause return Series(values, index=key_index, name=self._selection_name) # if we have date/time like in the original, then coerce dates diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py index e6f4f2f056058..8bebe5e8161a5 100644 --- a/pandas/core/groupby/ops.py +++ b/pandas/core/groupby/ops.py @@ -198,6 +198,9 @@ def apply(self, f, data, axis=0): f_name not in base.plotting_methods and hasattr(splitter, "fast_apply") and axis == 0 + # with MultiIndex, apply_frame_axis0 would raise InvalidApply + # TODO: can we make this check prettier? + and not splitter._get_sorted_data().index._has_complex_internals ): try: result_values, mutated = splitter.fast_apply(f, group_keys) @@ -207,11 +210,14 @@ def apply(self, f, data, axis=0): if len(result_values) == len(group_keys): return group_keys, result_values, mutated - except libreduction.InvalidApply: + except libreduction.InvalidApply as err: # Cannot fast apply on MultiIndex (_has_complex_internals). # This Exception is also raised if `f` triggers an exception # but it is preferable to raise the exception in Python. - pass + if "Let this error raise above us" not in str(err): + # TODO: can we infer anything about whether this is + # worth-retrying in pure-python? + raise except TypeError as err: if "Cannot convert" in str(err): # via apply_frame_axis0 if we pass a non-ndarray