用法:
dask.dataframe.to_numeric(arg, errors='raise', meta=None)
将参数转换为数值类型。
此文档字符串是从 pandas.to_numeric 复制而来的。
可能存在与 Dask 版本的一些不一致之处。
返回类型取决于输入。如果是标量则延迟,否则与输入相同。对于错误,仅允许 “raise” and “coerce”。
默认返回 dtype 是
float64
或int64
,具体取决于提供的数据。使用downcast
参数获取其他数据类型。请注意,如果传入的数字非常大,可能会出现精度损失。由于
ndarray
的内部限制,如果数字小于-9223372036854775808
(np.iinfo(np.int64).min) 或大于18446744073709551615
(np.iinfo(np.uint64).max) 被传入,它们很可能会被转换为浮点数,以便它们可以存储在ndarray
中。这些警告同样适用于Series
,因为它在内部利用了ndarray
。- arg:标量、列表、元组、一维数组或系列
要转换的参数。
- errors:{‘ignore’, ‘raise’, ‘coerce’},默认 ‘raise’
- 如果‘raise’,则无效解析将引发异常。
- 如果‘coerce’,则无效解析将设置为NaN。
- 如果‘ignore’,则无效解析将返回输入。
- downcast:str,默认无(Dask 中不支持)(Dask 中不支持)
可以是‘integer’, ‘signed’, ‘unsigned’, or ‘float’。如果不是 None,并且如果数据已成功转换为数字 dtype(或者如果数据以数字开头),则根据以下规则将该结果数据向下转换为可能的最小数字 dtype:
- ‘integer’ or ‘signed’:最小有符号 int dtype(最小值:np.int8)
- ‘unsigned’:最小的 unsigned int dtype(最小值:np.uint8)
- ‘float’:最小浮点 dtype(最小值:np.float32)
由于此行为与核心转换为数值是分开的,因此无论‘errors’ 输入的值如何,在向下转换期间引发的任何错误都将浮出水面。
此外,仅当结果数据的 dtype 的大小严格大于要转换为的 dtype 时才会发生向下转换,因此如果检查的 dtype 都不满足该规范,则不会对数据执行向下转换。
- ret
解析成功时的数值。返回类型取决于输入。如果是 Series,则为 Series,否则为 ndarray。
参数:
返回:
例子:
采取单独的系列并转换为数字,当被告知时强制
>>> 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
支持可空整数和浮点 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
相关用法
- Python dask.dataframe.to_records用法及代码示例
- Python dask.dataframe.to_datetime用法及代码示例
- Python dask.dataframe.to_hdf用法及代码示例
- Python dask.dataframe.to_parquet用法及代码示例
- Python dask.dataframe.to_sql用法及代码示例
- Python dask.dataframe.to_csv用法及代码示例
- Python dask.dataframe.tseries.resample.Resampler.agg用法及代码示例
- Python dask.dataframe.tseries.resample.Resampler.mean用法及代码示例
- Python dask.dataframe.Series.apply用法及代码示例
- Python dask.dataframe.DataFrame.applymap用法及代码示例
- Python dask.dataframe.Series.clip用法及代码示例
- Python dask.dataframe.Series.prod用法及代码示例
- Python dask.dataframe.Series.fillna用法及代码示例
- Python dask.dataframe.DataFrame.sub用法及代码示例
- Python dask.dataframe.compute用法及代码示例
- Python dask.dataframe.DataFrame.mod用法及代码示例
- Python dask.dataframe.Series.to_frame用法及代码示例
- Python dask.dataframe.read_table用法及代码示例
- Python dask.dataframe.read_hdf用法及代码示例
- Python dask.dataframe.Series.sum用法及代码示例
注:本文由纯净天空筛选整理自dask.org大神的英文原创作品 dask.dataframe.to_numeric。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。