pandas.to_numeric(arg, errors='raise', downcast=None)
[source]
Convert argument to a numeric type.
The default return dtype is float64
or int64
depending on the data supplied. Use the downcast
parameter to obtain other dtypes.
Please note that precision loss may occur if really large numbers are passed in. Due to the internal limitations of ndarray
, if numbers smaller than -9223372036854775808
(np.iinfo(np.int64).min) or larger than 18446744073709551615
(np.iinfo(np.uint64).max) are passed in, it is very likely they will be converted to float so that they can stored in an ndarray
. These warnings apply similarly to Series
since it internally leverages ndarray
.
Parameters: |
|
---|---|
Returns: |
|
See also
DataFrame.astype
to_datetime
to_timedelta
numpy.ndarray.astype
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 >>> pd.to_numeric(s, downcast='float') 0 1.0 1 2.0 2 -3.0 dtype: float32 >>> pd.to_numeric(s, downcast='signed') 0 1 1 2 2 -3 dtype: int8 >>> s = pd.Series(['apple', '1.0', '2', -3]) >>> pd.to_numeric(s, errors='ignore') 0 apple 1 1.0 2 2 3 -3 dtype: object >>> pd.to_numeric(s, errors='coerce') 0 NaN 1 1.0 2 2.0 3 -3.0 dtype: float64
© 2008–2012, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team
Licensed under the 3-clause BSD License.
https://pandas.pydata.org/pandas-docs/version/0.25.0/reference/api/pandas.to_numeric.html