Skip to content

Commit 341043d

Browse files
jbrockmendeljreback
authored andcommitted
CLN/REF: Remove _try_cast_result, _try_coerce_and_cast_result (#27764)
1 parent f669f94 commit 341043d

File tree

3 files changed

+13
-45
lines changed

3 files changed

+13
-45
lines changed

pandas/core/groupby/generic.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
from pandas.errors import AbstractMethodError
2222
from pandas.util._decorators import Appender, Substitution
2323

24-
from pandas.core.dtypes.cast import maybe_convert_objects, maybe_downcast_to_dtype
24+
from pandas.core.dtypes.cast import (
25+
maybe_convert_objects,
26+
maybe_downcast_numeric,
27+
maybe_downcast_to_dtype,
28+
)
2529
from pandas.core.dtypes.common import (
2630
ensure_int64,
2731
ensure_platform_int,
@@ -180,10 +184,8 @@ def _cython_agg_blocks(self, how, alt=None, numeric_only=True, min_count=-1):
180184
continue
181185
finally:
182186
if result is not no_result:
183-
dtype = block.values.dtype
184-
185187
# see if we can cast the block back to the original dtype
186-
result = block._try_coerce_and_cast_result(result, dtype=dtype)
188+
result = maybe_downcast_numeric(result, block.dtype)
187189
newb = block.make_block(result)
188190

189191
new_items.append(locs)

pandas/core/groupby/ops.py

+2
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,8 @@ def _cython_operation(self, kind, values, how, axis, min_count=-1, **kwargs):
591591

592592
if is_datetime64tz_dtype(orig_values.dtype):
593593
result = type(orig_values)(result.astype(np.int64), dtype=orig_values.dtype)
594+
elif is_datetimelike and kind == "aggregate":
595+
result = result.astype(orig_values.dtype)
594596

595597
return result, names
596598

pandas/core/internals/blocks.py

+5-41
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
find_common_type,
1919
infer_dtype_from,
2020
infer_dtype_from_scalar,
21+
maybe_downcast_numeric,
2122
maybe_downcast_to_dtype,
2223
maybe_infer_dtype_type,
2324
maybe_promote,
@@ -55,7 +56,6 @@
5556
ABCDataFrame,
5657
ABCDatetimeIndex,
5758
ABCExtensionArray,
58-
ABCIndexClass,
5959
ABCPandasArray,
6060
ABCSeries,
6161
)
@@ -685,28 +685,6 @@ def _can_hold_element(self, element):
685685
return issubclass(tipo.type, dtype)
686686
return isinstance(element, dtype)
687687

688-
def _try_cast_result(self, result, dtype=None):
689-
""" try to cast the result to our original type, we may have
690-
roundtripped thru object in the mean-time
691-
"""
692-
if dtype is None:
693-
dtype = self.dtype
694-
695-
if self.is_integer or self.is_bool or self.is_datetime:
696-
pass
697-
elif self.is_float and result.dtype == self.dtype:
698-
# protect against a bool/object showing up here
699-
if isinstance(dtype, str) and dtype == "infer":
700-
return result
701-
702-
# This is only reached via Block.setitem, where dtype is always
703-
# either "infer", self.dtype, or values.dtype.
704-
assert dtype == self.dtype, (dtype, self.dtype)
705-
return result
706-
707-
# may need to change the dtype here
708-
return maybe_downcast_to_dtype(result, dtype)
709-
710688
def _try_coerce_args(self, other):
711689
""" provide coercion to our input arguments """
712690

@@ -729,10 +707,6 @@ def _try_coerce_args(self, other):
729707

730708
return other
731709

732-
def _try_coerce_and_cast_result(self, result, dtype=None):
733-
result = self._try_cast_result(result, dtype=dtype)
734-
return result
735-
736710
def to_native_types(self, slicer=None, na_rep="nan", quoting=None, **kwargs):
737711
""" convert to our native types format, slicing if desired """
738712

@@ -925,8 +899,6 @@ def setitem(self, indexer, value):
925899
else:
926900
values[indexer] = value
927901

928-
# coerce and try to infer the dtypes of the result
929-
values = self._try_coerce_and_cast_result(values, dtype)
930902
if transpose:
931903
values = values.T
932904
block = self.make_block(values)
@@ -1444,10 +1416,6 @@ def func(cond, values, other):
14441416
if transpose:
14451417
result = result.T
14461418

1447-
# try to cast if requested
1448-
if try_cast:
1449-
result = self._try_cast_result(result)
1450-
14511419
return [self.make_block(result)]
14521420

14531421
# might need to separate out blocks
@@ -1459,7 +1427,7 @@ def func(cond, values, other):
14591427
for m in [mask, ~mask]:
14601428
if m.any():
14611429
taken = result.take(m.nonzero()[0], axis=axis)
1462-
r = self._try_cast_result(taken)
1430+
r = maybe_downcast_numeric(taken, self.dtype)
14631431
nb = self.make_block(r.T, placement=self.mgr_locs[m])
14641432
result_blocks.append(nb)
14651433

@@ -1692,9 +1660,6 @@ def putmask(self, mask, new, align=True, inplace=False, axis=0, transpose=False)
16921660
new_values[mask] = new
16931661
return [self.make_block(values=new_values)]
16941662

1695-
def _try_cast_result(self, result, dtype=None):
1696-
return result
1697-
16981663
def _get_unstack_items(self, unstacker, new_columns):
16991664
"""
17001665
Get the placement, values, and mask for a Block unstack.
@@ -1746,7 +1711,8 @@ def __init__(self, values, placement, ndim=None):
17461711
super().__init__(values, placement, ndim)
17471712

17481713
def _maybe_coerce_values(self, values):
1749-
"""Unbox to an extension array.
1714+
"""
1715+
Unbox to an extension array.
17501716
17511717
This will unbox an ExtensionArray stored in an Index or Series.
17521718
ExtensionArrays pass through. No dtype coercion is done.
@@ -1759,9 +1725,7 @@ def _maybe_coerce_values(self, values):
17591725
-------
17601726
ExtensionArray
17611727
"""
1762-
if isinstance(values, (ABCIndexClass, ABCSeries)):
1763-
values = values._values
1764-
return values
1728+
return extract_array(values)
17651729

17661730
@property
17671731
def _holder(self):

0 commit comments

Comments
 (0)