Skip to content

Commit bcecb38

Browse files
authored
REF: remove how arg from maybe_cast_result (#41000)
1 parent 7a42a8c commit bcecb38

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

pandas/core/dtypes/cast.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
ensure_str,
5656
is_bool,
5757
is_bool_dtype,
58-
is_categorical_dtype,
5958
is_complex,
6059
is_complex_dtype,
6160
is_datetime64_dtype,
@@ -79,6 +78,7 @@
7978
pandas_dtype,
8079
)
8180
from pandas.core.dtypes.dtypes import (
81+
CategoricalDtype,
8282
DatetimeTZDtype,
8383
ExtensionDtype,
8484
IntervalDtype,
@@ -359,15 +359,15 @@ def trans(x):
359359
return result
360360

361361

362-
def maybe_cast_result(
362+
def maybe_cast_pointwise_result(
363363
result: ArrayLike,
364364
dtype: DtypeObj,
365365
numeric_only: bool = False,
366-
how: str = "",
367366
same_dtype: bool = True,
368367
) -> ArrayLike:
369368
"""
370-
Try casting result to a different type if appropriate
369+
Try casting result of a pointwise operation back to the original dtype if
370+
appropriate.
371371
372372
Parameters
373373
----------
@@ -377,8 +377,6 @@ def maybe_cast_result(
377377
Input Series from which result was calculated.
378378
numeric_only : bool, default False
379379
Whether to cast only numerics or datetimes as well.
380-
how : str, default ""
381-
How the result was computed.
382380
same_dtype : bool, default True
383381
Specify dtype when calling _from_sequence
384382
@@ -387,12 +385,12 @@ def maybe_cast_result(
387385
result : array-like
388386
result maybe casted to the dtype.
389387
"""
390-
dtype = maybe_cast_result_dtype(dtype, how)
391388

392389
assert not is_scalar(result)
393390

394391
if isinstance(dtype, ExtensionDtype):
395-
if not is_categorical_dtype(dtype) and dtype.kind != "M":
392+
if not isinstance(dtype, (CategoricalDtype, DatetimeTZDtype)):
393+
# TODO: avoid this special-casing
396394
# We have to special case categorical so as not to upcast
397395
# things like counts back to categorical
398396

pandas/core/groupby/ops.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from pandas.util._decorators import cache_readonly
3737

3838
from pandas.core.dtypes.cast import (
39-
maybe_cast_result,
39+
maybe_cast_pointwise_result,
4040
maybe_cast_result_dtype,
4141
maybe_downcast_to_dtype,
4242
)
@@ -798,7 +798,7 @@ def _aggregate_series_pure_python(self, obj: Series, func: F):
798798
result[label] = res
799799

800800
out = lib.maybe_convert_objects(result, try_float=False)
801-
out = maybe_cast_result(out, obj.dtype, numeric_only=True)
801+
out = maybe_cast_pointwise_result(out, obj.dtype, numeric_only=True)
802802

803803
return out, counts
804804

pandas/core/series.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
from pandas.core.dtypes.cast import (
6161
convert_dtypes,
6262
maybe_box_native,
63-
maybe_cast_result,
63+
maybe_cast_pointwise_result,
6464
validate_numeric_casting,
6565
)
6666
from pandas.core.dtypes.common import (
@@ -3070,22 +3070,26 @@ def combine(self, other, func, fill_value=None) -> Series:
30703070
# so do this element by element
30713071
new_index = self.index.union(other.index)
30723072
new_name = ops.get_op_result_name(self, other)
3073-
new_values = []
3074-
for idx in new_index:
3073+
new_values = np.empty(len(new_index), dtype=object)
3074+
for i, idx in enumerate(new_index):
30753075
lv = self.get(idx, fill_value)
30763076
rv = other.get(idx, fill_value)
30773077
with np.errstate(all="ignore"):
3078-
new_values.append(func(lv, rv))
3078+
new_values[i] = func(lv, rv)
30793079
else:
30803080
# Assume that other is a scalar, so apply the function for
30813081
# each element in the Series
30823082
new_index = self.index
3083+
new_values = np.empty(len(new_index), dtype=object)
30833084
with np.errstate(all="ignore"):
3084-
new_values = [func(lv, other) for lv in self._values]
3085+
new_values[:] = [func(lv, other) for lv in self._values]
30853086
new_name = self.name
30863087

3087-
res_values = sanitize_array(new_values, None)
3088-
res_values = maybe_cast_result(res_values, self.dtype, same_dtype=False)
3088+
# try_float=False is to match _aggregate_series_pure_python
3089+
res_values = lib.maybe_convert_objects(new_values, try_float=False)
3090+
res_values = maybe_cast_pointwise_result(
3091+
res_values, self.dtype, same_dtype=False
3092+
)
30893093
return self._constructor(res_values, index=new_index, name=new_name)
30903094

30913095
def combine_first(self, other) -> Series:

0 commit comments

Comments
 (0)