Skip to content

Commit 3556865

Browse files
authored
REF: use lighter-weight casting function (#38322)
1 parent 5cafae7 commit 3556865

File tree

4 files changed

+9
-12
lines changed

4 files changed

+9
-12
lines changed

pandas/core/dtypes/cast.py

-4
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,6 @@ def maybe_downcast_numeric(result, dtype: DtypeObj, do_round: bool = False):
241241
# e.g. SparseDtype has no itemsize attr
242242
return result
243243

244-
if isinstance(result, list):
245-
# reached via groupby.agg._ohlc; really this should be handled earlier
246-
result = np.array(result)
247-
248244
def trans(x):
249245
if do_round:
250246
return x.round()

pandas/core/groupby/groupby.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class providing the base-class of operations.
5151
from pandas.errors import AbstractMethodError
5252
from pandas.util._decorators import Appender, Substitution, cache_readonly, doc
5353

54-
from pandas.core.dtypes.cast import maybe_downcast_to_dtype
54+
from pandas.core.dtypes.cast import maybe_downcast_numeric
5555
from pandas.core.dtypes.common import (
5656
ensure_float,
5757
is_bool_dtype,
@@ -1178,7 +1178,7 @@ def _python_agg_general(self, func, *args, **kwargs):
11781178
key = base.OutputKey(label=name, position=idx)
11791179

11801180
if is_numeric_dtype(obj.dtype):
1181-
result = maybe_downcast_to_dtype(result, obj.dtype)
1181+
result = maybe_downcast_numeric(result, obj.dtype)
11821182

11831183
if self.grouper._filter_empty_groups:
11841184
mask = counts.ravel() > 0
@@ -1188,7 +1188,7 @@ def _python_agg_general(self, func, *args, **kwargs):
11881188
if is_numeric_dtype(values.dtype):
11891189
values = ensure_float(values)
11901190

1191-
result = maybe_downcast_to_dtype(values[mask], result.dtype)
1191+
result = maybe_downcast_numeric(values[mask], result.dtype)
11921192

11931193
output[key] = result
11941194

pandas/core/indexes/interval.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
find_common_type,
2121
infer_dtype_from_scalar,
2222
maybe_box_datetimelike,
23-
maybe_downcast_to_dtype,
23+
maybe_downcast_numeric,
2424
)
2525
from pandas.core.dtypes.common import (
2626
ensure_platform_int,
@@ -1276,7 +1276,7 @@ def interval_range(
12761276
breaks = np.linspace(start, end, periods)
12771277
if all(is_integer(x) for x in com.not_none(start, end, freq)):
12781278
# np.linspace always produces float output
1279-
breaks = maybe_downcast_to_dtype(breaks, "int64")
1279+
breaks = maybe_downcast_numeric(breaks, np.dtype("int64"))
12801280
else:
12811281
# delegate to the appropriate range function
12821282
if isinstance(endpoint, Timestamp):

pandas/core/tools/numeric.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from pandas._libs import lib
44

5-
from pandas.core.dtypes.cast import maybe_downcast_to_dtype
5+
from pandas.core.dtypes.cast import maybe_downcast_numeric
66
from pandas.core.dtypes.common import (
77
ensure_object,
88
is_datetime_or_timedelta_dtype,
@@ -180,8 +180,9 @@ def to_numeric(arg, errors="raise", downcast=None):
180180
if typecodes is not None:
181181
# from smallest to largest
182182
for dtype in typecodes:
183-
if np.dtype(dtype).itemsize <= values.dtype.itemsize:
184-
values = maybe_downcast_to_dtype(values, dtype)
183+
dtype = np.dtype(dtype)
184+
if dtype.itemsize <= values.dtype.itemsize:
185+
values = maybe_downcast_numeric(values, dtype)
185186

186187
# successful conversion
187188
if values.dtype == dtype:

0 commit comments

Comments
 (0)