Skip to content

Commit f8191f8

Browse files
committed
fix logic in ensure_int_or_float
1 parent db8ed9b commit f8191f8

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

pandas/core/dtypes/common.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -160,24 +160,18 @@ def ensure_int_or_float(arr: ArrayLike, copy: bool = False) -> ArrayLike:
160160
will remain unchanged.
161161
"""
162162
# TODO: GH27506 potential bug with ExtensionArrays
163-
def call_right_astype(arr: ArrayLike, inttype: str) -> ArrayLike:
164-
if isinstance(arr, np.ndarray):
165-
return arr.astype(inttype, copy=copy, casting="safe")
166-
else:
167-
return arr.astype(inttype, copy=copy)
168-
169-
try:
170-
return call_right_astype(arr, "int64")
171-
except TypeError:
172-
pass
173-
try:
174-
return call_right_astype(arr, "uint64")
175-
except TypeError:
176-
if is_extension_array_dtype(arr.dtype):
177-
return cast("ExtensionArray", arr).to_numpy(
178-
dtype="float64", na_value=np.nan
179-
)
180-
return arr.astype("float64", copy=copy)
163+
if is_extension_array_dtype(arr.dtype):
164+
return cast("ExtensionArray", arr).to_numpy(dtype="float64", na_value=np.nan)
165+
else:
166+
assert isinstance(arr, np.ndarray) # For typing
167+
try:
168+
return arr.astype("int64", copy=copy, casting="safe")
169+
except TypeError:
170+
pass
171+
try:
172+
return arr.astype("uint64", copy=copy, casting="safe")
173+
except TypeError:
174+
return arr.astype("float64", copy=copy)
181175

182176

183177
def ensure_python_int(value: Union[int, np.integer]) -> int:

0 commit comments

Comments
 (0)