Skip to content

Commit 0c004fc

Browse files
authored
CLN: remove now-unnecesary coerce_to_dtypes (#38321)
1 parent 0aa598a commit 0c004fc

File tree

2 files changed

+4
-42
lines changed

2 files changed

+4
-42
lines changed

pandas/core/dtypes/cast.py

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -987,36 +987,6 @@ def coerce_indexer_dtype(indexer, categories):
987987
return ensure_int64(indexer)
988988

989989

990-
def coerce_to_dtypes(result: Sequence[Scalar], dtypes: Sequence[Dtype]) -> List[Scalar]:
991-
"""
992-
given a dtypes and a result set, coerce the result elements to the
993-
dtypes
994-
"""
995-
if len(result) != len(dtypes):
996-
raise AssertionError("_coerce_to_dtypes requires equal len arrays")
997-
998-
def conv(r, dtype):
999-
if np.any(isna(r)):
1000-
pass
1001-
elif dtype == DT64NS_DTYPE:
1002-
r = Timestamp(r)
1003-
elif dtype == TD64NS_DTYPE:
1004-
r = Timedelta(r)
1005-
elif dtype == np.bool_:
1006-
# messy. non 0/1 integers do not get converted.
1007-
if is_integer(r) and r not in [0, 1]:
1008-
return int(r)
1009-
r = bool(r)
1010-
elif dtype.kind == "f":
1011-
r = float(r)
1012-
elif dtype.kind == "i":
1013-
r = int(r)
1014-
1015-
return r
1016-
1017-
return [conv(r, dtype) for r, dtype in zip(result, dtypes)]
1018-
1019-
1020990
def astype_nansafe(
1021991
arr, dtype: DtypeObj, copy: bool = True, skipna: bool = False
1022992
) -> ArrayLike:

pandas/core/frame.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979

8080
from pandas.core.dtypes.cast import (
8181
cast_scalar_to_array,
82-
coerce_to_dtypes,
8382
construct_1d_arraylike_from_scalar,
8483
find_common_type,
8584
infer_dtype_from_scalar,
@@ -8817,11 +8816,9 @@ def _reduce(
88178816
labels = self._get_agg_axis(axis)
88188817
assert axis in [0, 1]
88198818

8820-
def func(values):
8821-
if is_extension_array_dtype(values.dtype):
8822-
return extract_array(values)._reduce(name, skipna=skipna, **kwds)
8823-
else:
8824-
return op(values, axis=axis, skipna=skipna, **kwds)
8819+
def func(values: np.ndarray):
8820+
# We only use this in the case that operates on self.values
8821+
return op(values, axis=axis, skipna=skipna, **kwds)
88258822

88268823
def blk_func(values):
88278824
if isinstance(values, ExtensionArray):
@@ -8859,10 +8856,6 @@ def _get_data() -> DataFrame:
88598856
out = df._constructor(res).iloc[0]
88608857
if out_dtype is not None:
88618858
out = out.astype(out_dtype)
8862-
if axis == 0 and is_object_dtype(out.dtype):
8863-
# GH#35865 careful to cast explicitly to object
8864-
nvs = coerce_to_dtypes(out.values, df.dtypes.iloc[np.sort(indexer)])
8865-
out[:] = np.array(nvs, dtype=object)
88668859
if axis == 0 and len(self) == 0 and name in ["sum", "prod"]:
88678860
# Even if we are object dtype, follow numpy and return
88688861
# float64, see test_apply_funcs_over_empty
@@ -8894,8 +8887,7 @@ def _get_data() -> DataFrame:
88948887
result = result.astype(np.float64)
88958888
except (ValueError, TypeError):
88968889
# try to coerce to the original dtypes item by item if we can
8897-
if axis == 0:
8898-
result = coerce_to_dtypes(result, data.dtypes)
8890+
pass
88998891

89008892
result = self._constructor_sliced(result, index=labels)
89018893
return result

0 commit comments

Comments
 (0)