Skip to content

Commit d6b058d

Browse files
jbrockmendeljreback
authored andcommitted
CLN: try/except cleanups (#28939)
1 parent 54b1151 commit d6b058d

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

pandas/core/apply.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -396,15 +396,11 @@ def wrap_results_for_axis(self):
396396
result = self.obj._constructor(data=results)
397397

398398
if not isinstance(results[0], ABCSeries):
399-
try:
399+
if len(result.index) == len(self.res_columns):
400400
result.index = self.res_columns
401-
except ValueError:
402-
pass
403401

404-
try:
402+
if len(result.columns) == len(self.res_index):
405403
result.columns = self.res_index
406-
except ValueError:
407-
pass
408404

409405
return result
410406

pandas/core/base.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from pandas.util._decorators import Appender, Substitution, cache_readonly
1717
from pandas.util._validators import validate_bool_kwarg
1818

19+
from pandas.core.dtypes.cast import is_nested_object
1920
from pandas.core.dtypes.common import (
2021
is_categorical_dtype,
2122
is_datetime64_ns_dtype,
@@ -566,32 +567,37 @@ def _aggregate_multiple_funcs(self, arg, _level, _axis):
566567
# degenerate case
567568
if obj.ndim == 1:
568569
for a in arg:
570+
colg = self._gotitem(obj.name, ndim=1, subset=obj)
569571
try:
570-
colg = self._gotitem(obj.name, ndim=1, subset=obj)
571-
results.append(colg.aggregate(a))
572+
new_res = colg.aggregate(a)
572573

573-
# make sure we find a good name
574-
name = com.get_callable_name(a) or a
575-
keys.append(name)
576574
except (TypeError, DataError):
577575
pass
578576
except SpecificationError:
579577
raise
578+
else:
579+
results.append(new_res)
580+
581+
# make sure we find a good name
582+
name = com.get_callable_name(a) or a
583+
keys.append(name)
580584

581585
# multiples
582586
else:
583587
for index, col in enumerate(obj):
588+
colg = self._gotitem(col, ndim=1, subset=obj.iloc[:, index])
584589
try:
585-
colg = self._gotitem(col, ndim=1, subset=obj.iloc[:, index])
586-
results.append(colg.aggregate(arg))
587-
keys.append(col)
590+
new_res = colg.aggregate(arg)
588591
except (TypeError, DataError):
589592
pass
590593
except ValueError:
591594
# cannot aggregate
592595
continue
593596
except SpecificationError:
594597
raise
598+
else:
599+
results.append(new_res)
600+
keys.append(col)
595601

596602
# if we are empty
597603
if not len(results):
@@ -604,7 +610,6 @@ def _aggregate_multiple_funcs(self, arg, _level, _axis):
604610
# we are concatting non-NDFrame objects,
605611
# e.g. a list of scalars
606612

607-
from pandas.core.dtypes.cast import is_nested_object
608613
from pandas import Series
609614

610615
result = Series(results, index=keys, name=self.name)

pandas/core/groupby/generic.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -505,9 +505,7 @@ def true_and_notna(x, *args, **kwargs):
505505
indices = [
506506
self._get_index(name) for name, group in self if true_and_notna(group)
507507
]
508-
except ValueError:
509-
raise TypeError("the filter must return a boolean result")
510-
except TypeError:
508+
except (ValueError, TypeError):
511509
raise TypeError("the filter must return a boolean result")
512510

513511
filtered = self._apply_filter(indices, dropna)
@@ -1052,8 +1050,8 @@ def _aggregate_item_by_item(self, func, *args, **kwargs):
10521050
data = obj[item]
10531051
colg = SeriesGroupBy(data, selection=item, grouper=self.grouper)
10541052

1053+
cast = self._transform_should_cast(func)
10551054
try:
1056-
cast = self._transform_should_cast(func)
10571055

10581056
result[item] = colg.aggregate(func, *args, **kwargs)
10591057
if cast:

0 commit comments

Comments
 (0)