diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 12974d56dacdc..4627100161b40 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -241,10 +241,6 @@ def maybe_downcast_numeric(result, dtype: DtypeObj, do_round: bool = False): # e.g. SparseDtype has no itemsize attr return result - if isinstance(result, list): - # reached via groupby.agg._ohlc; really this should be handled earlier - result = np.array(result) - def trans(x): if do_round: return x.round() diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 23f0e178130be..947f18901775b 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -51,7 +51,7 @@ class providing the base-class of operations. from pandas.errors import AbstractMethodError from pandas.util._decorators import Appender, Substitution, cache_readonly, doc -from pandas.core.dtypes.cast import maybe_downcast_to_dtype +from pandas.core.dtypes.cast import maybe_downcast_numeric from pandas.core.dtypes.common import ( ensure_float, is_bool_dtype, @@ -1178,7 +1178,7 @@ def _python_agg_general(self, func, *args, **kwargs): key = base.OutputKey(label=name, position=idx) if is_numeric_dtype(obj.dtype): - result = maybe_downcast_to_dtype(result, obj.dtype) + result = maybe_downcast_numeric(result, obj.dtype) if self.grouper._filter_empty_groups: mask = counts.ravel() > 0 @@ -1188,7 +1188,7 @@ def _python_agg_general(self, func, *args, **kwargs): if is_numeric_dtype(values.dtype): values = ensure_float(values) - result = maybe_downcast_to_dtype(values[mask], result.dtype) + result = maybe_downcast_numeric(values[mask], result.dtype) output[key] = result diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index 2f86d9c20bfe8..0c5fbef6cb087 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -20,7 +20,7 @@ find_common_type, infer_dtype_from_scalar, maybe_box_datetimelike, - maybe_downcast_to_dtype, + maybe_downcast_numeric, ) from pandas.core.dtypes.common import ( ensure_platform_int, @@ -1274,7 +1274,7 @@ def interval_range( breaks = np.linspace(start, end, periods) if all(is_integer(x) for x in com.not_none(start, end, freq)): # np.linspace always produces float output - breaks = maybe_downcast_to_dtype(breaks, "int64") + breaks = maybe_downcast_numeric(breaks, np.dtype("int64")) else: # delegate to the appropriate range function if isinstance(endpoint, Timestamp): diff --git a/pandas/core/tools/numeric.py b/pandas/core/tools/numeric.py index 4af32b219d380..dd7373927ed9b 100644 --- a/pandas/core/tools/numeric.py +++ b/pandas/core/tools/numeric.py @@ -2,7 +2,7 @@ from pandas._libs import lib -from pandas.core.dtypes.cast import maybe_downcast_to_dtype +from pandas.core.dtypes.cast import maybe_downcast_numeric from pandas.core.dtypes.common import ( ensure_object, is_datetime_or_timedelta_dtype, @@ -180,8 +180,9 @@ def to_numeric(arg, errors="raise", downcast=None): if typecodes is not None: # from smallest to largest for dtype in typecodes: - if np.dtype(dtype).itemsize <= values.dtype.itemsize: - values = maybe_downcast_to_dtype(values, dtype) + dtype = np.dtype(dtype) + if dtype.itemsize <= values.dtype.itemsize: + values = maybe_downcast_numeric(values, dtype) # successful conversion if values.dtype == dtype: