gpt4 book ai didi

python - 为什么 pd.to_numeric `errors=' '` is equivalent to ` 错误 ='coerce' `

转载 作者:太空宇宙 更新时间:2023-11-03 13:06:40 26 4
gpt4 key购买 nike

我在 python 3.7 和 pandas 0.24.2

设置:

s = pd.Series(['10', '12', '15', '20', 'A', '31', 'C', 'D'])

In [36]: s
Out[36]:
0 10
1 12
2 15
3 20
4 A
5 31
6 C
7 D
dtype: object

to_numeric with errors='coerce'

pd.to_numeric(s, errors='coerce')

Out[37]:
0 10.0
1 12.0
2 15.0
3 20.0
4 NaN
5 31.0
6 NaN
7 NaN
dtype: float64

to_numeric with errors=''(空字符串)

pd.to_numeric(s, errors='')

Out[38]:
0 10.0
1 12.0
2 15.0
3 20.0
4 NaN
5 31.0
6 NaN
7 NaN
dtype: float64

to_numeric 与 errors='ljljalklag'。即,随机字符串

pd.to_numeric(s, errors='ljljalklag')

Out[39]:
0 10.0
1 12.0
2 15.0
3 20.0
4 NaN
5 31.0
6 NaN
7 NaN
dtype: float64

换句话说,将除字符串raiseignore之外的任何字符串传递给pd.to_numericerrors参数等同于 errors='coerce'

这是功能还是错误?

最佳答案

这已在 0.25.0 版中修复以验证 errors 关键字(参见 #26394 )。

0.25.0 中的新行为:

In [1]: import pandas as pd; pd.__version__
Out[1]: '0.25.0'

In [2]: pd.to_numeric([1, 'a', 2.2], errors='foo')
---------------------------------------------------------------------------
ValueError: invalid error value specified

0.24.2 中的先前行为:

In [1]: import pandas as pd; pd.__version__
Out[1]: '0.24.2'

In [2]: pd.to_numeric([1, 'a', 2.2], errors='foo')
Out[2]: array([1. , nan, 2.2])

关于python - 为什么 pd.to_numeric `errors=' '` is equivalent to ` 错误 ='coerce' `,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57286501/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com