Skip to content

Commit b2e34fb

Browse files
committed
Fixup
1 parent d6edeff commit b2e34fb

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

pandas/core/groupby/ops.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,11 @@ def agg_series(
961961
if isinstance(obj._values, ArrowExtensionArray):
962962
from pandas.core.dtypes.common import is_string_dtype
963963

964-
if not is_string_dtype(obj.dtype) or is_string_dtype(npvalues):
964+
# 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+
):
965969
out = maybe_cast_pointwise_result(
966970
npvalues, obj.dtype, numeric_only=True, same_dtype=preserve_dtype
967971
)

0 commit comments

Comments
 (0)