pandas.to_numeric#

pandas.to_numeric(arg, errors='raise', downcast=None, dtype_backend=_NoDefault.no_default)[源代码]#

将参数转换为数字类型。

默认返回的 dtype 为 float64int64,具体取决于提供的数据。使用 downcast 参数可以获得其他 dtype。

请注意,如果传入非常大的数字,可能会发生精度损失。由于 ndarray 的内部限制,如果传入小于 -9223372036854775808 (np.iinfo(np.int64).min) 或大于 18446744073709551615 (np.iinfo(np.uint64).max) 的数字,很可能它们会被转换为浮点数,以便存储在 ndarray 中。这些警告同样适用于 Series,因为它内部利用了 ndarray

Parameters:
arg标量、列表、元组、一维数组或 Series

要转换的参数。

errors{‘ignore’, ‘raise’, ‘coerce’},默认为 ‘raise’
  • 如果为 ‘raise’,则无效解析将引发异常。

  • 如果为 ‘coerce’,则无效解析将设置为 NaN。

  • 如果为 ‘ignore’,则无效解析将返回输入。

在 2.2 版本发生变更.

“ignore” 已弃用。请改用显式捕获异常。

downcaststr,默认 None

可以是 ‘integer’、’signed’、’unsigned’ 或 ‘float’。如果不是 None,并且数据已成功转换为数值 dtype(或数据本身就是数值类型的),则根据以下规则将结果数据缩小到最小的数值 dtype:

  • ‘integer’ 或 ‘signed’: 最小的有符号整数 dtype (最小:np.int8)

  • ‘unsigned’: 最小的无符号整数 dtype (最小:np.uint8)

  • ‘float’: 最小的浮点数 dtype (最小:np.float32)

由于此行为与核心数值转换是分开的,因此在缩小过程中引发的任何错误都将显示出来,而与 ‘errors’ 输入的值无关。

此外,仅当结果数据的 dtype 大于要转换到的 dtype 时,才会进行缩小。因此,如果检查的 dtype 都不满足此规范,则不会对数据执行缩小操作。

dtype_backend{‘numpy_nullable’, ‘pyarrow’}, 默认 ‘numpy_nullable’

应用于结果 DataFrame 的后端数据类型(仍处于实验阶段)。行为如下:

  • "numpy_nullable":返回支持可空 dtype 的 DataFrame (默认)。

  • "pyarrow":返回 pyarrow 支持的可空 ArrowDtype DataFrame。

在 2.0 版本加入.

Returns:
ret

解析成功时为数值类型。返回类型取决于输入。如果是 Series,则返回 Series;否则返回 ndarray。

参见

DataFrame.astype

将参数转换为指定的dtype。

to_datetime

将参数转换为 datetime。

to_timedelta

将参数转换为 timedelta。

numpy.ndarray.astype

将 numpy 数组强制转换为指定类型。

DataFrame.convert_dtypes

转换dtype。

Examples

获取单独的 Series 并转换为数值类型,根据指示进行强制转换

>>> 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='coerce')
0    NaN
1    1.0
2    2.0
3   -3.0
dtype: float64

支持缩小可空的整数和浮点数 dtype:

>>> s = pd.Series([1, 2, 3], dtype="Int64")
>>> pd.to_numeric(s, downcast="integer")
0    1
1    2
2    3
dtype: Int8
>>> s = pd.Series([1.0, 2.1, 3.0], dtype="Float64")
>>> pd.to_numeric(s, downcast="float")
0    1.0
1    2.1
2    3.0
dtype: Float32