From e3f3ba177b9cda2334285480df4b2fa929889ab8 Mon Sep 17 00:00:00 2001 From: Brock Date: Sat, 5 Dec 2020 13:26:20 -0800 Subject: [PATCH 1/3] REF: use lighter-weight casting function --- pandas/core/dtypes/cast.py | 4 ---- pandas/core/groupby/groupby.py | 6 +++--- pandas/core/indexes/interval.py | 4 ++-- pandas/core/reshape/pivot.py | 4 ++-- pandas/core/tools/numeric.py | 7 ++++--- 5 files changed, 11 insertions(+), 14 deletions(-) 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/reshape/pivot.py b/pandas/core/reshape/pivot.py index 40496a5b8671b..845d36d870e1d 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -16,7 +16,7 @@ from pandas._typing import FrameOrSeriesUnion, Label from pandas.util._decorators import Appender, Substitution -from pandas.core.dtypes.cast import maybe_downcast_to_dtype +from pandas.core.dtypes.cast import maybe_downcast_numeric, maybe_downcast_to_dtype from pandas.core.dtypes.common import is_integer_dtype, is_list_like, is_scalar from pandas.core.dtypes.generic import ABCDataFrame, ABCSeries @@ -126,7 +126,7 @@ def pivot_table( and v in agged and not is_integer_dtype(agged[v]) ): - agged[v] = maybe_downcast_to_dtype(agged[v], data[v].dtype) + agged[v] = maybe_downcast_numeric(agged[v], data[v].dtype) table = agged 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: From 6e14000346d602f0f1e7e333b62faff7e220db4f Mon Sep 17 00:00:00 2001 From: Brock Date: Sat, 5 Dec 2020 14:12:00 -0800 Subject: [PATCH 2/3] doctest fixup --- pandas/core/reshape/pivot.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index 845d36d870e1d..fe4158e95093a 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -124,6 +124,7 @@ def pivot_table( v in data and is_integer_dtype(data[v]) and v in agged + and not isinstance(agged[v], ABCDataFrame) and not is_integer_dtype(agged[v]) ): agged[v] = maybe_downcast_numeric(agged[v], data[v].dtype) From 7561b4f779ebc32a72baa858fd3373e43c1cbd63 Mon Sep 17 00:00:00 2001 From: Brock Date: Sat, 5 Dec 2020 18:13:47 -0800 Subject: [PATCH 3/3] Doctest troubleshoot --- pandas/core/reshape/pivot.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index fe4158e95093a..40496a5b8671b 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -16,7 +16,7 @@ from pandas._typing import FrameOrSeriesUnion, Label from pandas.util._decorators import Appender, Substitution -from pandas.core.dtypes.cast import maybe_downcast_numeric, maybe_downcast_to_dtype +from pandas.core.dtypes.cast import maybe_downcast_to_dtype from pandas.core.dtypes.common import is_integer_dtype, is_list_like, is_scalar from pandas.core.dtypes.generic import ABCDataFrame, ABCSeries @@ -124,10 +124,9 @@ def pivot_table( v in data and is_integer_dtype(data[v]) and v in agged - and not isinstance(agged[v], ABCDataFrame) and not is_integer_dtype(agged[v]) ): - agged[v] = maybe_downcast_numeric(agged[v], data[v].dtype) + agged[v] = maybe_downcast_to_dtype(agged[v], data[v].dtype) table = agged