Skip to content

Commit 48b3fee

Browse files
jbrockmendelproost
authored andcommitted
CLN: preempt TypeError for EAs in groupby agg_series (pandas-dev#29186)
1 parent 3e73cc1 commit 48b3fee

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

pandas/core/groupby/generic.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,12 @@ def aggregate(self, func=None, *args, **kwargs):
264264
return self._python_agg_general(func, *args, **kwargs)
265265
except (AssertionError, TypeError):
266266
raise
267-
except Exception:
267+
except (ValueError, KeyError, AttributeError, IndexError):
268+
# TODO: IndexError can be removed here following GH#29106
269+
# TODO: AttributeError is caused by _index_data hijinx in
270+
# libreduction, can be removed after GH#29160
271+
# TODO: KeyError is raised in _python_agg_general,
272+
# see see test_groupby.test_basic
268273
result = self._aggregate_named(func, *args, **kwargs)
269274

270275
index = Index(sorted(result), name=self.grouper.names[0])

pandas/core/groupby/ops.py

+10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
is_complex_dtype,
2727
is_datetime64_any_dtype,
2828
is_datetime64tz_dtype,
29+
is_extension_array_dtype,
2930
is_integer_dtype,
3031
is_numeric_dtype,
3132
is_sparse,
@@ -659,6 +660,12 @@ def _transform(
659660
return result
660661

661662
def agg_series(self, obj, func):
663+
if is_extension_array_dtype(obj.dtype) and obj.dtype.kind != "M":
664+
# _aggregate_series_fast would raise TypeError when
665+
# calling libreduction.Slider
666+
# TODO: is the datetime64tz case supposed to go through here?
667+
return self._aggregate_series_pure_python(obj, func)
668+
662669
try:
663670
return self._aggregate_series_fast(obj, func)
664671
except AssertionError:
@@ -683,6 +690,8 @@ def agg_series(self, obj, func):
683690
def _aggregate_series_fast(self, obj, func):
684691
func = self._is_builtin_func(func)
685692

693+
# TODO: pre-empt this, also pre-empt get_result raising TypError if we pass a EA
694+
# for EAs backed by ndarray we may have a performant workaround
686695
if obj.index._has_complex_internals:
687696
raise TypeError("Incompatible index for Cython grouper")
688697

@@ -717,6 +726,7 @@ def _aggregate_series_pure_python(self, obj, func):
717726
result[label] = res
718727

719728
result = lib.maybe_convert_objects(result, try_float=0)
729+
# TODO: try_cast back to EA?
720730
return result, counts
721731

722732

0 commit comments

Comments
 (0)