Skip to content

Commit 278cd70

Browse files
Update numeric.py
1 parent 22f12fc commit 278cd70

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

pandas/core/tools/numeric.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,24 @@ def to_numeric(
271271
if values.dtype == dtype:
272272
break
273273

274+
# Fallback: if we requested an unsigned downcast but did not
275+
# successfully convert (e.g. because the data was float64 after
276+
# parsing large Python ints), attempt a direct cast to uint64 as a
277+
# last resort. This addresses GH#14422 where `to_numeric` failed to
278+
# downcast `[0, 9223372036854775808]` to ``uint64``.
279+
if (
280+
downcast == "unsigned"
281+
and values.dtype.kind == "f" # still a float dtype
282+
and (not len(values) or np.all(np.mod(values, 1) == 0)) # integral values
283+
and (not len(values) or np.min(values) >= 0)
284+
and (not len(values) or np.max(values) <= np.iinfo(np.uint64).max)
285+
):
286+
try:
287+
values = values.astype(np.uint64)
288+
except (OverflowError, ValueError):
289+
# If casting is unsafe, keep original dtype
290+
pass
291+
274292
# GH33013: for IntegerArray, BooleanArray & FloatingArray need to reconstruct
275293
# masked array
276294
if (mask is not None or new_mask is not None) and not is_string_dtype(values.dtype):

0 commit comments

Comments
 (0)