Skip to content

Commit c294bdd

Browse files
authored
CLN: remove ensure_int_or_float (#41011)
1 parent 5f1307e commit c294bdd

File tree

2 files changed

+5
-49
lines changed

2 files changed

+5
-49
lines changed

pandas/core/dtypes/common.py

-45
Original file line numberDiff line numberDiff line change
@@ -128,51 +128,6 @@ def ensure_str(value: Union[bytes, Any]) -> str:
128128
return value
129129

130130

131-
def ensure_int_or_float(arr: ArrayLike, copy: bool = False) -> np.ndarray:
132-
"""
133-
Ensure that an dtype array of some integer dtype
134-
has an int64 dtype if possible.
135-
If it's not possible, potentially because of overflow,
136-
convert the array to float64 instead.
137-
138-
Parameters
139-
----------
140-
arr : array-like
141-
The array whose data type we want to enforce.
142-
copy: bool
143-
Whether to copy the original array or reuse
144-
it in place, if possible.
145-
146-
Returns
147-
-------
148-
out_arr : The input array cast as int64 if
149-
possible without overflow.
150-
Otherwise the input array cast to float64.
151-
152-
Notes
153-
-----
154-
If the array is explicitly of type uint64 the type
155-
will remain unchanged.
156-
"""
157-
# TODO: GH27506 potential bug with ExtensionArrays
158-
try:
159-
# error: Unexpected keyword argument "casting" for "astype"
160-
return arr.astype("int64", copy=copy, casting="safe") # type: ignore[call-arg]
161-
except TypeError:
162-
pass
163-
try:
164-
# error: Unexpected keyword argument "casting" for "astype"
165-
return arr.astype("uint64", copy=copy, casting="safe") # type: ignore[call-arg]
166-
except TypeError:
167-
if is_extension_array_dtype(arr.dtype):
168-
# pandas/core/dtypes/common.py:168: error: Item "ndarray" of
169-
# "Union[ExtensionArray, ndarray]" has no attribute "to_numpy" [union-attr]
170-
return arr.to_numpy( # type: ignore[union-attr]
171-
dtype="float64", na_value=np.nan
172-
)
173-
return arr.astype("float64", copy=copy)
174-
175-
176131
def ensure_python_int(value: Union[int, np.integer]) -> int:
177132
"""
178133
Ensure that a value is a python int.

pandas/core/groupby/ops.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
from pandas.core.dtypes.common import (
4444
ensure_float64,
4545
ensure_int64,
46-
ensure_int_or_float,
4746
ensure_platform_int,
4847
is_bool_dtype,
4948
is_categorical_dtype,
@@ -582,7 +581,7 @@ def _ea_wrap_cython_operation(
582581

583582
elif is_integer_dtype(values.dtype) or is_bool_dtype(values.dtype):
584583
# IntegerArray or BooleanArray
585-
values = ensure_int_or_float(values)
584+
values = values.to_numpy("float64", na_value=np.nan)
586585
res_values = self._cython_operation(
587586
kind, values, how, axis, min_count, **kwargs
588587
)
@@ -660,9 +659,11 @@ def _cython_operation(
660659
values = values.view("int64")
661660
is_numeric = True
662661
elif is_bool_dtype(dtype):
663-
values = ensure_int_or_float(values)
662+
values = values.astype("int64")
664663
elif is_integer_dtype(dtype):
665-
values = ensure_int_or_float(values)
664+
# e.g. uint8 -> uint64, int16 -> int64
665+
dtype = dtype.kind + "8"
666+
values = values.astype(dtype, copy=False)
666667
elif is_numeric:
667668
if not is_complex_dtype(dtype):
668669
values = ensure_float64(values)

0 commit comments

Comments
 (0)