diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 184fa3a2b4204..b077c7889049f 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -1000,9 +1000,6 @@ def _cython_transform( except NotImplementedError: continue - if self._transform_should_cast(how): - result = maybe_cast_result(result, obj, how=how) - key = base.OutputKey(label=name, position=idx) output[key] = result @@ -1081,12 +1078,12 @@ def _cython_agg_general( assert len(agg_names) == result.shape[1] for result_column, result_name in zip(result.T, agg_names): key = base.OutputKey(label=result_name, position=idx) - output[key] = maybe_cast_result(result_column, obj, how=how) + output[key] = result_column idx += 1 else: assert result.ndim == 1 key = base.OutputKey(label=name, position=idx) - output[key] = maybe_cast_result(result, obj, how=how) + output[key] = result idx += 1 if not output: diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py index ded5f610b850e..62002f62afbcd 100644 --- a/pandas/core/groupby/ops.py +++ b/pandas/core/groupby/ops.py @@ -28,7 +28,11 @@ from pandas.errors import AbstractMethodError from pandas.util._decorators import cache_readonly -from pandas.core.dtypes.cast import maybe_cast_result +from pandas.core.dtypes.cast import ( + maybe_cast_result, + maybe_cast_result_dtype, + maybe_downcast_to_dtype, +) from pandas.core.dtypes.common import ( ensure_float, ensure_float64, @@ -620,8 +624,11 @@ def _cython_operation( if swapped: result = result.swapaxes(0, axis) - if is_datetimelike and kind == "aggregate": - result = result.astype(orig_values.dtype) + if how not in base.cython_cast_blocklist: + # e.g. if we are int64 and need to restore to datetime64/timedelta64 + # "rank" is the only member of cython_cast_blocklist we get here + dtype = maybe_cast_result_dtype(orig_values.dtype, how) + result = maybe_downcast_to_dtype(result, dtype) return result, names