Skip to content

Commit b8fcde1

Browse files
Update numeric.py
1 parent 73c4fce commit b8fcde1

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

pandas/core/tools/numeric.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
from __future__ import annotations
2+
from pandas._libs import libmissing
3+
from pandas.core.dtypes.common import is_string_dtype
4+
from pandas.core.dtypes.missing import isna
25

36
from typing import (
47
TYPE_CHECKING,
@@ -41,6 +44,16 @@
4144
npt,
4245
)
4346

47+
def parse_numeric(value):
48+
if isinstance(value, str):
49+
try:
50+
return int(value, 0) # Automatically detect radix
51+
except ValueError:
52+
try:
53+
return float(value)
54+
except ValueError:
55+
return libmissing.NA
56+
return value
4457

4558
def to_numeric(
4659
arg,
@@ -161,6 +174,7 @@ def to_numeric(
161174
2 3.0
162175
dtype: Float32
163176
"""
177+
164178
if downcast not in (None, "integer", "signed", "unsigned", "float"):
165179
raise ValueError("invalid downcasting method provided")
166180

@@ -215,14 +229,25 @@ def to_numeric(
215229
else:
216230
values = ensure_object(values)
217231
coerce_numeric = errors != "raise"
218-
values, new_mask = lib.maybe_convert_numeric( # type: ignore[call-overload]
219-
values,
220-
set(),
221-
coerce_numeric=coerce_numeric,
222-
convert_to_masked_nullable=dtype_backend is not lib.no_default
223-
or isinstance(values_dtype, StringDtype)
224-
and values_dtype.na_value is libmissing.NA,
225-
)
232+
parsed_values = []
233+
new_mask = []
234+
235+
for idx, x in enumerate(values):
236+
parsed_value = parse_numeric(x)
237+
if libmissing.checknull(parsed_value):
238+
if errors == 'raise':
239+
raise ValueError(f"Unable to parse string '{x}' at position {idx}")
240+
elif errors == 'coerce':
241+
parsed_values.append(libmissing.NA)
242+
new_mask.append(True)
243+
continue
244+
else:
245+
parsed_values.append(parsed_value)
246+
new_mask.append(False)
247+
248+
values = np.array(parsed_values, dtype=object)
249+
new_mask = np.array(new_mask, dtype=bool)
250+
226251

227252
if new_mask is not None:
228253
# Remove unnecessary values, is expected later anyway and enables

0 commit comments

Comments
 (0)