From b8fcde11bb5ad30fee4f76b2e0c355ce4a58e627 Mon Sep 17 00:00:00 2001 From: Staudinger0325 <147128423+Staudinger0325@users.noreply.github.com> Date: Sat, 14 Sep 2024 19:38:01 +0800 Subject: [PATCH 1/3] Update numeric.py --- pandas/core/tools/numeric.py | 41 +++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/pandas/core/tools/numeric.py b/pandas/core/tools/numeric.py index 982851d0557c3..9eee594d4d987 100644 --- a/pandas/core/tools/numeric.py +++ b/pandas/core/tools/numeric.py @@ -1,4 +1,7 @@ from __future__ import annotations +from pandas._libs import libmissing +from pandas.core.dtypes.common import is_string_dtype +from pandas.core.dtypes.missing import isna from typing import ( TYPE_CHECKING, @@ -41,6 +44,16 @@ npt, ) +def parse_numeric(value): + if isinstance(value, str): + try: + return int(value, 0) # Automatically detect radix + except ValueError: + try: + return float(value) + except ValueError: + return libmissing.NA + return value def to_numeric( arg, @@ -161,6 +174,7 @@ def to_numeric( 2 3.0 dtype: Float32 """ + if downcast not in (None, "integer", "signed", "unsigned", "float"): raise ValueError("invalid downcasting method provided") @@ -215,14 +229,25 @@ def to_numeric( else: values = ensure_object(values) coerce_numeric = errors != "raise" - values, new_mask = lib.maybe_convert_numeric( # type: ignore[call-overload] - values, - set(), - coerce_numeric=coerce_numeric, - convert_to_masked_nullable=dtype_backend is not lib.no_default - or isinstance(values_dtype, StringDtype) - and values_dtype.na_value is libmissing.NA, - ) + parsed_values = [] + new_mask = [] + + for idx, x in enumerate(values): + parsed_value = parse_numeric(x) + if libmissing.checknull(parsed_value): + if errors == 'raise': + raise ValueError(f"Unable to parse string '{x}' at position {idx}") + elif errors == 'coerce': + parsed_values.append(libmissing.NA) + new_mask.append(True) + continue + else: + parsed_values.append(parsed_value) + new_mask.append(False) + + values = np.array(parsed_values, dtype=object) + new_mask = np.array(new_mask, dtype=bool) + if new_mask is not None: # Remove unnecessary values, is expected later anyway and enables From 2f6e281eb481d9bf910607a36c33001891c7dfad Mon Sep 17 00:00:00 2001 From: Staudinger0325 <147128423+Staudinger0325@users.noreply.github.com> Date: Sat, 14 Sep 2024 19:45:07 +0800 Subject: [PATCH 2/3] Update numeric.py --- pandas/core/tools/numeric.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/tools/numeric.py b/pandas/core/tools/numeric.py index 9eee594d4d987..a0e84e1241209 100644 --- a/pandas/core/tools/numeric.py +++ b/pandas/core/tools/numeric.py @@ -1,7 +1,5 @@ from __future__ import annotations -from pandas._libs import libmissing -from pandas.core.dtypes.common import is_string_dtype -from pandas.core.dtypes.missing import isna + from typing import ( TYPE_CHECKING, @@ -36,7 +34,9 @@ from pandas.core.arrays import BaseMaskedArray from pandas.core.arrays.string_ import StringDtype - +from pandas._libs import libmissing +from pandas.core.dtypes.common import is_string_dtype +from pandas.core.dtypes.missing import isna if TYPE_CHECKING: from pandas._typing import ( DateTimeErrorChoices, From 6c3324d1f3b61ca8367845371f85a024cb8f1061 Mon Sep 17 00:00:00 2001 From: Staudinger0325 <147128423+Staudinger0325@users.noreply.github.com> Date: Sat, 14 Sep 2024 20:09:31 +0800 Subject: [PATCH 3/3] Update numeric.py --- pandas/core/tools/numeric.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/tools/numeric.py b/pandas/core/tools/numeric.py index a0e84e1241209..30a2462ec87ee 100644 --- a/pandas/core/tools/numeric.py +++ b/pandas/core/tools/numeric.py @@ -34,9 +34,9 @@ from pandas.core.arrays import BaseMaskedArray from pandas.core.arrays.string_ import StringDtype -from pandas._libs import libmissing + from pandas.core.dtypes.common import is_string_dtype -from pandas.core.dtypes.missing import isna + if TYPE_CHECKING: from pandas._typing import ( DateTimeErrorChoices,