Should I use Series.replace or Series.str.replace?
Let's say I have a series like this:
u = pandas.Series(['foo', 'bar'])
I want to do a simple regex replacement.
Should I approve u.replace('o+', '', regex = True)
or u.str.replace('o+', '')
?
I have never observed performance differences and looking at the docs Series.replace
seems to be much more general than that Series.str.replace
. So what is the meaning of the latter?
source to share
In my opinion, you are right.
str.replace
works only if the string
values ββare in the column, otherwise an error.
replace
works with values ββtoo string
and no string
is therefore more general. Also, if the parameter regex=True
replaces substrings, if not, then it replaces the value Series
.
This answer explains better.
source to share