From 278cd705f7251eb8834cbbf43b58e67f1feecba4 Mon Sep 17 00:00:00 2001 From: "S. M. Mohiuddin Khan Shiam" Date: Thu, 3 Jul 2025 16:23:45 +0600 Subject: [PATCH] Update numeric.py --- pandas/core/tools/numeric.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pandas/core/tools/numeric.py b/pandas/core/tools/numeric.py index bc45343d6e2d3..4c0356d7de024 100644 --- a/pandas/core/tools/numeric.py +++ b/pandas/core/tools/numeric.py @@ -271,6 +271,24 @@ def to_numeric( if values.dtype == dtype: break + # Fallback: if we requested an unsigned downcast but did not + # successfully convert (e.g. because the data was float64 after + # parsing large Python ints), attempt a direct cast to uint64 as a + # last resort. This addresses GH#14422 where `to_numeric` failed to + # downcast `[0, 9223372036854775808]` to ``uint64``. + if ( + downcast == "unsigned" + and values.dtype.kind == "f" # still a float dtype + and (not len(values) or np.all(np.mod(values, 1) == 0)) # integral values + and (not len(values) or np.min(values) >= 0) + and (not len(values) or np.max(values) <= np.iinfo(np.uint64).max) + ): + try: + values = values.astype(np.uint64) + except (OverflowError, ValueError): + # If casting is unsafe, keep original dtype + pass + # GH33013: for IntegerArray, BooleanArray & FloatingArray need to reconstruct # masked array if (mask is not None or new_mask is not None) and not is_string_dtype(values.dtype):