From 62df9f3699bcabf6c4d7d009ea12663af1c59706 Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 30 Nov 2020 20:18:42 -0800 Subject: [PATCH 1/4] REF: collect casting closer to wrapped call --- pandas/core/groupby/groupby.py | 7 ++----- pandas/core/groupby/ops.py | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 6 deletions(-) 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..d478e6c0bc8a6 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, @@ -623,6 +627,15 @@ def _cython_operation( if is_datetimelike and kind == "aggregate": result = result.astype(orig_values.dtype) + if kind == "transform": + filled_series = self.size().fillna(0) + if filled_series.gt(0).any() and how not in base.cython_cast_blocklist: + dtype = maybe_cast_result_dtype(orig_values.dtype, how) + result = maybe_downcast_to_dtype(result, dtype) + else: + dtype = maybe_cast_result_dtype(orig_values.dtype, how) + result = maybe_downcast_to_dtype(result, dtype) + return result, names def _aggregate( From d80d74d315c5e209283f9281d727668b768e6778 Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 1 Dec 2020 20:19:28 -0800 Subject: [PATCH 2/4] simplify --- pandas/core/groupby/ops.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py index d478e6c0bc8a6..f012fc9a617e1 100644 --- a/pandas/core/groupby/ops.py +++ b/pandas/core/groupby/ops.py @@ -627,12 +627,8 @@ def _cython_operation( if is_datetimelike and kind == "aggregate": result = result.astype(orig_values.dtype) - if kind == "transform": - filled_series = self.size().fillna(0) - if filled_series.gt(0).any() and how not in base.cython_cast_blocklist: - dtype = maybe_cast_result_dtype(orig_values.dtype, how) - result = maybe_downcast_to_dtype(result, dtype) - else: + if how not in base.cython_cast_blocklist: + # "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) From 87d41e61ada09dc6b1a2e62ee1bb1a903ce97baa Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 2 Dec 2020 07:13:57 -0800 Subject: [PATCH 3/4] CLN: remove redundant cast check --- pandas/core/groupby/ops.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py index f012fc9a617e1..4e686ee8658df 100644 --- a/pandas/core/groupby/ops.py +++ b/pandas/core/groupby/ops.py @@ -624,9 +624,6 @@ 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: # "rank" is the only member of cython_cast_blocklist we get here dtype = maybe_cast_result_dtype(orig_values.dtype, how) From 9b3567f3fc1b11c885e13d8bd279e9c1f4d98045 Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 2 Dec 2020 07:14:52 -0800 Subject: [PATCH 4/4] commenet --- pandas/core/groupby/ops.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py index 4e686ee8658df..62002f62afbcd 100644 --- a/pandas/core/groupby/ops.py +++ b/pandas/core/groupby/ops.py @@ -625,6 +625,7 @@ def _cython_operation( result = result.swapaxes(0, axis) 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)