Skip to content

CLN: try/except cleanups #28939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 14 additions & 9 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -566,32 +567,37 @@ 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:
# cannot aggregate
continue
except SpecificationError:
raise
else:
results.append(new_res)
keys.append(col)

# if we are empty
if not len(results):
Expand All @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down