modin.pandas.Series.str.replace¶

Series.str.replace(pat, repl, n=- 1, case=None, flags=0, regex=False)[source]¶

Replace each occurrence of pattern/regex in the Series/Index.

Equivalent to str.replace() or re.sub(), depending on the regex value.

Parameters:
  • pat (str) – String can be a character sequence or regular expression.

  • repl (str or callable) – Replacement string or a callable. The callable is passed the regex match object and must return a replacement string to be used. See re.sub().

  • n (int, default -1 (all)) – Number of replacements to make from start.

  • case (bool, default None) – Determines if replace is case sensitive: - If True, case sensitive (the default if pat is a string) - Set to False for case insensitive - Cannot be set if pat is a compiled regex.

  • flags (int, default 0 (no flags)) – Regex module flags, e.g. re.IGNORECASE. Cannot be set if pat is a compiled regex.

  • regex (bool, default False) – Determines if the passed-in pattern is a regular expression: - If True, assumes the passed-in pattern is a regular expression. - If False, treats the pattern as a literal string - Cannot be set to False if pat is a compiled regex or repl is a callable.

Returns:

A copy of the object with all matching occurrences of pat replaced by repl.

Return type:

Series or Index of object

Raises:

ValueError –

  • if regex is False and repl is a callable or pat is a compiled regex - if pat is a compiled regex and case or flags is set

Notes

When pat is a compiled regex, all flags should be included in the compiled regex. Use of case, flags, or regex=False with a compiled regex will raise an error.

Examples

When pat is a string and regex is True, the given pat is compiled as a regex. When repl is a string, it replaces matching regex patterns as with re.sub(). NaN value(s) in the Series are left as is:

>>> pd.Series(['foo', 'fuz', np.nan]).str.replace('f.', 'ba', regex=True)
0     bao
1     baz
2    None
dtype: object
Copy

When pat is a string and regex is False, every pat is replaced with repl as with str.replace():

>>> pd.Series(['f.o', 'fuz', np.nan]).str.replace('f.', 'ba', regex=False)
0     bao
1     fuz
2    None
dtype: object
Copy

Using a compiled regex with flags