- StringMethods.replace(pat: Union[str, Sequence], repl: Union[str, Sequence], n: int = -1, case=None, flags: int = 0, regex: bool = True) SeriesOrIndex #
Replace occurrences of pattern/regex in the Series/Index with some other string. Equivalent to str.replace() or re.sub().
- patstr or list-like
String(s) to be replaced as a character sequence or regular expression.
- replstr or list-like
String(s) to be used as replacement.
- nint, default -1 (all)
Number of replacements to make from the start.
- regexbool, default True
If True, assumes the pattern is a regular expression. If False, treats the pattern as a literal string.
- Series/Index of str dtype
A copy of the object with all matching occurrences of pat replaced by repl.
The parameters case and flags are not yet supported and will raise a NotImplementedError if anything other than the default value is set.
>>> import cudf >>> s = cudf.Series(['foo', 'fuz', None]) >>> s 0 foo 1 fuz 2 <NA> dtype: object
When pat is a string and regex is True (the default), 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:
>>> s.str.replace('f.', 'ba', regex=True) 0 bao 1 baz 2 <NA> dtype: object
When pat is a string and regex is False, every pat is replaced with repl as with
>>> s.str.replace('f.', 'ba', regex=False) 0 foo 1 fuz 2 <NA> dtype: object