From 8e9177f0f8caae345f703f292421962d06264502 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 12 Oct 2019 09:23:48 -0700 Subject: [PATCH] CLN: try/except cleanups --- pandas/core/apply.py | 8 ++------ pandas/core/base.py | 23 ++++++++++++++--------- pandas/core/groupby/generic.py | 6 ++---- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 605d179e7c652..91f3e878c3807 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -396,15 +396,11 @@ def wrap_results_for_axis(self): result = self.obj._constructor(data=results) if not isinstance(results[0], ABCSeries): - try: + if len(result.index) == len(self.res_columns): result.index = self.res_columns - except ValueError: - pass - try: + if len(result.columns) == len(self.res_index): result.columns = self.res_index - except ValueError: - pass return result diff --git a/pandas/core/base.py b/pandas/core/base.py index 4d5b20c56df5a..2d798dd15ad24 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -16,6 +16,7 @@ from pandas.util._decorators import Appender, Substitution, cache_readonly from pandas.util._validators import validate_bool_kwarg +from pandas.core.dtypes.cast import is_nested_object from pandas.core.dtypes.common import ( is_categorical_dtype, is_datetime64_ns_dtype, @@ -566,25 +567,27 @@ def _aggregate_multiple_funcs(self, arg, _level, _axis): # degenerate case if obj.ndim == 1: for a in arg: + colg = self._gotitem(obj.name, ndim=1, subset=obj) try: - colg = self._gotitem(obj.name, ndim=1, subset=obj) - results.append(colg.aggregate(a)) + new_res = colg.aggregate(a) - # make sure we find a good name - name = com.get_callable_name(a) or a - keys.append(name) except (TypeError, DataError): pass except SpecificationError: raise + else: + results.append(new_res) + + # make sure we find a good name + name = com.get_callable_name(a) or a + keys.append(name) # multiples else: for index, col in enumerate(obj): + colg = self._gotitem(col, ndim=1, subset=obj.iloc[:, index]) try: - colg = self._gotitem(col, ndim=1, subset=obj.iloc[:, index]) - results.append(colg.aggregate(arg)) - keys.append(col) + new_res = colg.aggregate(arg) except (TypeError, DataError): pass except ValueError: @@ -592,6 +595,9 @@ def _aggregate_multiple_funcs(self, arg, _level, _axis): continue except SpecificationError: raise + else: + results.append(new_res) + keys.append(col) # if we are empty if not len(results): @@ -604,7 +610,6 @@ def _aggregate_multiple_funcs(self, arg, _level, _axis): # we are concatting non-NDFrame objects, # e.g. a list of scalars - from pandas.core.dtypes.cast import is_nested_object from pandas import Series result = Series(results, index=keys, name=self.name) diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index 5200d33c6a1fb..7be11696b7d45 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -505,9 +505,7 @@ def true_and_notna(x, *args, **kwargs): indices = [ self._get_index(name) for name, group in self if true_and_notna(group) ] - except ValueError: - raise TypeError("the filter must return a boolean result") - except TypeError: + except (ValueError, TypeError): raise TypeError("the filter must return a boolean result") filtered = self._apply_filter(indices, dropna) @@ -1052,8 +1050,8 @@ def _aggregate_item_by_item(self, func, *args, **kwargs): data = obj[item] colg = SeriesGroupBy(data, selection=item, grouper=self.grouper) + cast = self._transform_should_cast(func) try: - cast = self._transform_should_cast(func) result[item] = colg.aggregate(func, *args, **kwargs) if cast: