Skip to content

Commit

Permalink
CLN: remove now-unnecesary coerce_to_dtypes (#38321)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored Dec 7, 2020
1 parent 0aa598a commit 0c004fc
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 42 deletions.
30 changes: 0 additions & 30 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 4 additions & 12 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0c004fc

Please sign in to comment.