MCPcopy
hub / github.com/pandas-dev/pandas / to_numeric

Function to_numeric

pandas/core/tools/numeric.py:51–327  ·  view source on GitHub ↗

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,
)

Source from the content-addressed store, hash-verified

49
50@set_module("pandas")
51def 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

Callers 15

time_from_floatMethod · 0.90
time_from_numeric_strMethod · 0.90
time_from_strMethod · 0.90
time_downcastMethod · 0.90
coerceFunction · 0.90
melt_stubFunction · 0.90
test_emptyFunction · 0.90
test_seriesFunction · 0.90
test_series_numericFunction · 0.90

Calls 15

isnaMethod · 0.95
dropnaMethod · 0.95
__arrow_array__Method · 0.95
check_dtype_backendFunction · 0.90
needs_i8_conversionFunction · 0.90
is_numeric_dtypeFunction · 0.90
maybe_downcast_numericFunction · 0.90
is_string_dtypeFunction · 0.90
is_integer_dtypeFunction · 0.90
is_bool_dtypeFunction · 0.90
ArrowExtensionArrayClass · 0.90
IndexClass · 0.90

Tested by 15

test_emptyFunction · 0.72
test_seriesFunction · 0.72
test_series_numericFunction · 0.72
test_errorFunction · 0.72
test_ignore_errorFunction · 0.72
test_bool_handlingFunction · 0.72
test_listFunction · 0.72
test_list_numericFunction · 0.72
test_numericFunction · 0.72
test_all_nanFunction · 0.72
test_type_checkFunction · 0.72
test_scalarFunction · 0.72