Skip to content Skip to sidebar Skip to footer

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'

Solution 1:

The use of None is problematic there. If you pass None for an argument, it will use the default value for that (docs):

None

The sole value of types.NoneType. None is frequently used to represent the absence of a value, as when default arguments are not passed to a function.

So s.replace('', None) is the same as s.replace(''). Apparently the default action when no value is passed is to forward fill the Series. Instead, you can use np.nan:

pd.Series(['', '1', '2', '', '4', '', '6']).replace('', np.nan)
Out: 
0    NaN
11223    NaN
445    NaN
66
dtype: object

Or pass a dict:

s.replace({'': None})
Out: 
0None11223None445None66
dtype: object

Post a Comment for "Unexpected Pandas.series.replace() Behavior"