Convert argument to a numeric type. If the input is already of a numeric dtype, the dtype will be preserved. For non-numeric inputs, the default return dtype is `float64` or `int64` depending on the data supplied. Use the `downcast` parameter to obtain other dtypes. Please
(
arg,
errors: DateTimeErrorChoices = "raise",
downcast: Literal["integer", "signed", "unsigned", "float"] | None = None,
dtype_backend: DtypeBackend | lib.NoDefault = lib.no_default,
)
| 49 | |
| 50 | @set_module("pandas") |
| 51 | def to_numeric( |
| 52 | arg, |
| 53 | errors: DateTimeErrorChoices = "raise", |
| 54 | downcast: Literal["integer", "signed", "unsigned", "float"] | None = None, |
| 55 | dtype_backend: DtypeBackend | lib.NoDefault = lib.no_default, |
| 56 | ): |
| 57 | """ |
| 58 | Convert argument to a numeric type. |
| 59 | |
| 60 | If the input is already of a numeric dtype, the dtype will be preserved. |
| 61 | For non-numeric inputs, the default return dtype is `float64` or `int64` |
| 62 | depending on the data supplied. Use the `downcast` parameter |
| 63 | to obtain other dtypes. |
| 64 | |
| 65 | Please note that precision loss may occur if really large numbers |
| 66 | are passed in. Due to the internal limitations of `ndarray`, if |
| 67 | numbers smaller than `-9223372036854775808` (np.iinfo(np.int64).min) |
| 68 | or larger than `18446744073709551615` (np.iinfo(np.uint64).max) are |
| 69 | passed in, it is very likely they will be converted to float so that |
| 70 | they can be stored in an `ndarray`. These warnings apply similarly to |
| 71 | `Series` since it internally leverages `ndarray`. |
| 72 | |
| 73 | Parameters |
| 74 | ---------- |
| 75 | arg : scalar, list, tuple, 1-d array, or Series |
| 76 | Argument to be converted. |
| 77 | |
| 78 | errors : {'raise', 'coerce'}, default 'raise' |
| 79 | - If 'raise', then invalid parsing will raise an exception. |
| 80 | - If 'coerce', then invalid parsing will be set as NaN. |
| 81 | |
| 82 | downcast : str, default None |
| 83 | Can be 'integer', 'signed', 'unsigned', or 'float'. |
| 84 | If not None, and if the data has been successfully cast to a |
| 85 | numerical dtype (or if the data was numeric to begin with), |
| 86 | downcast that resulting data to the smallest numerical dtype |
| 87 | possible according to the following rules: |
| 88 | |
| 89 | - 'integer' or 'signed': smallest signed int dtype (min.: np.int8) |
| 90 | - 'unsigned': smallest unsigned int dtype (min.: np.uint8) |
| 91 | - 'float': smallest float dtype (min.: np.float32) |
| 92 | |
| 93 | As this behaviour is separate from the core conversion to |
| 94 | numeric values, any errors raised during the downcasting |
| 95 | will be surfaced regardless of the value of the 'errors' input. |
| 96 | |
| 97 | In addition, downcasting will only occur if the size |
| 98 | of the resulting data's dtype is strictly larger than |
| 99 | the dtype it is to be cast to, so if none of the dtypes |
| 100 | checked satisfy that specification, no downcasting will be |
| 101 | performed on the data. |
| 102 | |
| 103 | dtype_backend : {'numpy_nullable', 'pyarrow'} |
| 104 | Back-end data type applied to the resultant :class:`DataFrame` |
| 105 | (still experimental). If not specified, the default behavior |
| 106 | is to not use nullable data types. If specified, the behavior |
| 107 | is as follows: |
| 108 |