File tree Expand file tree Collapse file tree 1 file changed +18
-0
lines changed Expand file tree Collapse file tree 1 file changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -271,6 +271,24 @@ def to_numeric(
271
271
if values .dtype == dtype :
272
272
break
273
273
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
+
274
292
# GH33013: for IntegerArray, BooleanArray & FloatingArray need to reconstruct
275
293
# masked array
276
294
if (mask is not None or new_mask is not None ) and not is_string_dtype (values .dtype ):
You can’t perform that action at this time.
0 commit comments