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?

+3


source to share


1 answer


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.

+3


source







All Articles