Skip to content

Commit d4605cb

Browse files
jbrockmendelproost
authored andcommitted
CLN: prevent libreduction TypeError (pandas-dev#29228)
1 parent c883298 commit d4605cb

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

pandas/core/groupby/generic.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,7 @@ def _aggregate_item_by_item(self, func, *args, **kwargs):
11041104
# raised in _aggregate_named, handle at higher level
11051105
# see test_apply_with_mutated_index
11061106
raise
1107+
# otherwise we get here from an AttributeError in _make_wrapper
11071108
cannot_agg.append(item)
11081109
continue
11091110

@@ -1466,7 +1467,8 @@ def _transform_item_by_item(self, obj, wrapper):
14661467
output[col] = self[col].transform(wrapper)
14671468
except AssertionError:
14681469
raise
1469-
except Exception:
1470+
except TypeError:
1471+
# e.g. trying to call nanmean with string values
14701472
pass
14711473
else:
14721474
inds.append(i)

pandas/core/groupby/groupby.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -641,12 +641,15 @@ def curried(x):
641641
# if we don't have this method to indicated to aggregate to
642642
# mark this column as an error
643643
try:
644-
return self._aggregate_item_by_item(name, *args, **kwargs)
644+
result = self._aggregate_item_by_item(name, *args, **kwargs)
645+
assert self.obj.ndim == 2
646+
return result
645647
except AttributeError:
646648
# e.g. SparseArray has no flags attr
647649
# FIXME: 'SeriesGroupBy' has no attribute '_aggregate_item_by_item'
648650
# occurs in idxmax() case
649651
# in tests.groupby.test_function.test_non_cython_api
652+
assert self.obj.ndim == 1
650653
raise ValueError
651654

652655
wrapper.__name__ = name

pandas/core/groupby/ops.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -669,13 +669,16 @@ def agg_series(self, obj, func):
669669
if is_extension_array_dtype(obj.dtype) and obj.dtype.kind != "M":
670670
# _aggregate_series_fast would raise TypeError when
671671
# calling libreduction.Slider
672+
# TODO: can we get a performant workaround for EAs backed by ndarray?
672673
# TODO: is the datetime64tz case supposed to go through here?
673674
return self._aggregate_series_pure_python(obj, func)
674675

676+
elif obj.index._has_complex_internals:
677+
# MultiIndex; Pre-empt TypeError in _aggregate_series_fast
678+
return self._aggregate_series_pure_python(obj, func)
679+
675680
try:
676681
return self._aggregate_series_fast(obj, func)
677-
except AssertionError:
678-
raise
679682
except ValueError as err:
680683
if "No result." in str(err):
681684
# raised in libreduction
@@ -685,12 +688,6 @@ def agg_series(self, obj, func):
685688
pass
686689
else:
687690
raise
688-
except TypeError as err:
689-
if "ndarray" in str(err):
690-
# raised in libreduction if obj's values is no ndarray
691-
pass
692-
else:
693-
raise
694691
return self._aggregate_series_pure_python(obj, func)
695692

696693
def _aggregate_series_fast(self, obj, func):

0 commit comments

Comments
 (0)