[英]Unexpected pandas.Series.replace() behavior
Given this - 鉴于这种 -
import pandas as pd
s = pd.Series(['', '1', '2', '', '4', '', '6'])
Why does this - 为什么这个 -
s.replace('', None).values
Result in this - 结果如下 -
array(['', '1', '2', '2', '4', '4', '6'], dtype=object)
When I would expect this - 当我期待这个 -
array([None, '1', '2', None, '4', None, '6'], dtype=object)
The use of None is problematic there. 使用None是有问题的。 If you pass None for an argument, it will use the default value for that ( docs ):
如果为参数传递None,它将使用默认值( docs ):
None
没有
The sole value of types.NoneType.
types.NoneType的唯一值。 None is frequently used to represent the absence of a value, as when default arguments are not passed to a function.
None通常用于表示缺少值,因为默认参数未传递给函数。
So s.replace('', None)
is the same as s.replace('')
. 所以
s.replace('', None)
与s.replace('')
。 Apparently the default action when no value is passed is to forward fill the Series. 显然,没有传递值时的默认操作是转发填充系列。 Instead, you can use np.nan:
相反,你可以使用np.nan:
pd.Series(['', '1', '2', '', '4', '', '6']).replace('', np.nan)
Out:
0 NaN
1 1
2 2
3 NaN
4 4
5 NaN
6 6
dtype: object
Or pass a dict: 或通过一个字典:
s.replace({'': None})
Out:
0 None
1 1
2 2
3 None
4 4
5 None
6 6
dtype: object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.