Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 37 additions & 17 deletions pandas/core/tools/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,23 @@
)


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,
errors: DateTimeErrorChoices = "raise",
downcast: Literal["integer", "signed", "unsigned", "float"] | None = None,
dtype_backend: DtypeBackend | lib.NoDefault = lib.no_default,
arg,
errors: DateTimeErrorChoices = "raise",
downcast: Literal["integer", "signed", "unsigned", "float"] | None = None,
dtype_backend: DtypeBackend | lib.NoDefault = lib.no_default,
):
"""
Convert argument to a numeric type.
Expand Down Expand Up @@ -214,25 +226,33 @@ def to_numeric(
values = values.view(np.int64)
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_values):
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
# downcasting
values = values[~new_mask]
elif (
dtype_backend is not lib.no_default
and new_mask is None
or isinstance(values_dtype, StringDtype)
and values_dtype.na_value is libmissing.NA
dtype_backend is not lib.no_default
and new_mask is None
or isinstance(values_dtype, StringDtype)
and values_dtype.na_value is libmissing.NA
):
new_mask = np.zeros(values.shape, dtype=np.bool_)

Expand Down
Loading