Skip to content

REF: consolidate result-casting in _cython_operation #38237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
13 changes: 10 additions & 3 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down