Skip to content

REF: use lighter-weight casting function #38322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down
7 changes: 4 additions & 3 deletions pandas/core/tools/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down