From 0c004fcb6c3c2ab4d638d9665d13044e08fb2bc6 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 7 Dec 2020 05:47:47 -0800 Subject: [PATCH] CLN: remove now-unnecesary coerce_to_dtypes (#38321) --- pandas/core/dtypes/cast.py | 30 ------------------------------ pandas/core/frame.py | 16 ++++------------ 2 files changed, 4 insertions(+), 42 deletions(-) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 12b5017cf34a0..0a2d4c22f8a4b 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -987,36 +987,6 @@ def coerce_indexer_dtype(indexer, categories): return ensure_int64(indexer) -def coerce_to_dtypes(result: Sequence[Scalar], dtypes: Sequence[Dtype]) -> List[Scalar]: - """ - given a dtypes and a result set, coerce the result elements to the - dtypes - """ - if len(result) != len(dtypes): - raise AssertionError("_coerce_to_dtypes requires equal len arrays") - - def conv(r, dtype): - if np.any(isna(r)): - pass - elif dtype == DT64NS_DTYPE: - r = Timestamp(r) - elif dtype == TD64NS_DTYPE: - r = Timedelta(r) - elif dtype == np.bool_: - # messy. non 0/1 integers do not get converted. - if is_integer(r) and r not in [0, 1]: - return int(r) - r = bool(r) - elif dtype.kind == "f": - r = float(r) - elif dtype.kind == "i": - r = int(r) - - return r - - return [conv(r, dtype) for r, dtype in zip(result, dtypes)] - - def astype_nansafe( arr, dtype: DtypeObj, copy: bool = True, skipna: bool = False ) -> ArrayLike: diff --git a/pandas/core/frame.py b/pandas/core/frame.py index f710660d6ad8e..360d61a30a13a 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -79,7 +79,6 @@ from pandas.core.dtypes.cast import ( cast_scalar_to_array, - coerce_to_dtypes, construct_1d_arraylike_from_scalar, find_common_type, infer_dtype_from_scalar, @@ -8817,11 +8816,9 @@ def _reduce( labels = self._get_agg_axis(axis) assert axis in [0, 1] - def func(values): - if is_extension_array_dtype(values.dtype): - return extract_array(values)._reduce(name, skipna=skipna, **kwds) - else: - return op(values, axis=axis, skipna=skipna, **kwds) + def func(values: np.ndarray): + # We only use this in the case that operates on self.values + return op(values, axis=axis, skipna=skipna, **kwds) def blk_func(values): if isinstance(values, ExtensionArray): @@ -8859,10 +8856,6 @@ def _get_data() -> DataFrame: out = df._constructor(res).iloc[0] if out_dtype is not None: out = out.astype(out_dtype) - if axis == 0 and is_object_dtype(out.dtype): - # GH#35865 careful to cast explicitly to object - nvs = coerce_to_dtypes(out.values, df.dtypes.iloc[np.sort(indexer)]) - out[:] = np.array(nvs, dtype=object) if axis == 0 and len(self) == 0 and name in ["sum", "prod"]: # Even if we are object dtype, follow numpy and return # float64, see test_apply_funcs_over_empty @@ -8894,8 +8887,7 @@ def _get_data() -> DataFrame: result = result.astype(np.float64) except (ValueError, TypeError): # try to coerce to the original dtypes item by item if we can - if axis == 0: - result = coerce_to_dtypes(result, data.dtypes) + pass result = self._constructor_sliced(result, index=labels) return result