modin.pandas.to_numeric

modin.pandas.to_numeric(arg: Scalar | Series | ArrayConvertible, errors: Literal['ignore', 'raise', 'coerce'] = 'raise', downcast: Literal['integer', 'signed', 'unsigned', 'float'] | None = None) Series | Scalar | None[source]

Convert argument to a numeric type.

If the input arg type is already a numeric type, the return dtype will be the original type; otherwise, the return dtype is float.

Parameters:
  • arg (scalar, list, tuple, 1-d array, or Series) – Argument to be converted.

  • errors ({'ignore', 'raise', 'coerce'}, default 'raise') –

    • If ‘raise’, then invalid parsing will raise an exception.

    • If ‘coerce’, then invalid parsing will be set as NaN.

    • If ‘ignore’, then invalid parsing will return the input.

  • downcast (str, default None) – downcast is ignored in Snowflake backend.

Returns:

Numeric if parsing succeeded. Return type depends on input. Series if arg is not scalar.

Return type:

ret

See also

DataFrame.astype

Cast argument to a specified dtype.

to_datetime

Convert argument to datetime.

to_timedelta

Convert argument to timedelta.

numpy.ndarray.astype

Cast a numpy array to a specified type.

DataFrame.convert_dtypes

Convert dtypes.

Examples

Take separate series and convert to numeric, coercing when told to

>>> s = pd.Series(['1.0', '2', -3])
>>> pd.to_numeric(s)
0    1.0
1    2.0
2   -3.0
dtype: float64
Copy

Note: to_numeric always converts non-numeric values to floats >>> s = pd.Series([‘1’, ‘2’, ‘-3’]) >>> pd.to_numeric(s) 0 1.0 1 2.0 2 -3.0 dtype: float64 >>> pd.to_numeric(s, downcast=’float’) # downcast is ignored 0 1.0 1 2.0 2 -3.0 dtype: float64 >>> pd.to_numeric(s, downcast=’signed’) # downcast is ignored 0 1.0 1 2.0 2 -3.0 dtype: float64 >>> s = pd.Series([‘apple’, ‘1.0’, ‘2’, -3]) >>> pd.to_numeric(s, errors=’coerce’) 0 NaN 1 1.0 2 2.0 3 -3.0 dtype: float64