Skip to content

Commit 09e8472

Browse files
committed
Added EA check and passed through _from_sequence
1 parent 5a0c91e commit 09e8472

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

pandas/core/groupby/groupby.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class providing the base-class of operations.
2727

2828
from pandas.core.dtypes.common import (
2929
is_numeric_dtype,
30+
is_extension_array_dtype,
3031
is_scalar,
3132
ensure_float)
3233
from pandas.core.dtypes.cast import maybe_downcast_to_dtype
@@ -758,6 +759,18 @@ def _try_cast(self, result, obj, numeric_only=False):
758759
else:
759760
dtype = obj.dtype
760761

762+
if is_extension_array_dtype(obj.dtype):
763+
# The function can return something of any type, so check
764+
# if the type is compatible with the calling EA.
765+
try:
766+
result = obj.values._from_sequence(result)
767+
except Exception:
768+
# https://github.com/pandas-dev/pandas/issues/22850
769+
# pandas has no control over what 3rd-party ExtensionArrays
770+
# do in _values_from_sequence. We still want ops to work
771+
# though, so we catch any regular Exception.
772+
pass
773+
761774
if not is_scalar(result):
762775
if numeric_only and is_numeric_dtype(dtype) or not numeric_only:
763776
result = maybe_downcast_to_dtype(result, dtype)

0 commit comments

Comments
 (0)