|
1 | 1 | 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 |
2 | 5 |
|
3 | 6 | from typing import (
|
4 | 7 | TYPE_CHECKING,
|
|
41 | 44 | npt,
|
42 | 45 | )
|
43 | 46 |
|
| 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 |
44 | 57 |
|
45 | 58 | def to_numeric(
|
46 | 59 | arg,
|
@@ -161,6 +174,7 @@ def to_numeric(
|
161 | 174 | 2 3.0
|
162 | 175 | dtype: Float32
|
163 | 176 | """
|
| 177 | + |
164 | 178 | if downcast not in (None, "integer", "signed", "unsigned", "float"):
|
165 | 179 | raise ValueError("invalid downcasting method provided")
|
166 | 180 |
|
@@ -215,14 +229,25 @@ def to_numeric(
|
215 | 229 | else:
|
216 | 230 | values = ensure_object(values)
|
217 | 231 | 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 | + |
226 | 251 |
|
227 | 252 | if new_mask is not None:
|
228 | 253 | # Remove unnecessary values, is expected later anyway and enables
|
|
0 commit comments