We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d6edeff commit b2e34fbCopy full SHA for b2e34fb
pandas/core/groupby/ops.py
@@ -961,7 +961,11 @@ def agg_series(
961
if isinstance(obj._values, ArrowExtensionArray):
962
from pandas.core.dtypes.common import is_string_dtype
963
964
- if not is_string_dtype(obj.dtype) or is_string_dtype(npvalues):
+ # When obj.dtype is a string, any object can be cast. Only do so if the
965
+ # UDF returned strings or NA values.
966
+ if not is_string_dtype(obj.dtype) or is_string_dtype(
967
+ npvalues[~isna(npvalues)]
968
+ ):
969
out = maybe_cast_pointwise_result(
970
npvalues, obj.dtype, numeric_only=True, same_dtype=preserve_dtype
971
)
0 commit comments