【发布时间】:2019-12-08 17:19:23
【问题描述】:
我在 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 与 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 与 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
换句话说,将除字符串raise、ignore 之外的任何字符串传递给pd.to_numeric 的errors 参数都等效于errors='coerce'。
这是功能还是错误?
【问题讨论】:
标签: python pandas python-3.6