xarray.core.accessor_str.StringAccessor.replace#

StringAccessor.replace(pat, repl, n=-1, case=None, flags=0, regex=True)[source]#

Replace occurrences of pattern/regex in the array with some string.

If pat, repl, or β€˜n` is array-like, they are broadcast against the array and applied elementwise.

Parameters:
  • pat (str or re.Pattern or array-like of str or re.Pattern) – String can be a character sequence or regular expression. If array-like, it is broadcast.

  • repl (str or callable() or array-like of 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(). If array-like, it is broadcast.

  • n (int or array of int, default: -1) – Number of replacements to make from start. Use -1 to replace all. If array-like, it is broadcast.

  • case (bool, default: True) – If True, case sensitive. Cannot be set if pat is a compiled regex. Equivalent to setting the re.IGNORECASE flag.

  • flags (int, default: 0) – Flags to pass through to the re module, e.g. re.IGNORECASE. see compilation-flags. 0 means no flags. Flags can be combined with the bitwise or operator |. Cannot be set if pat is a compiled regex.

  • regex (bool, default: True) – 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:

replaced (same type as values) – A copy of the object with all matching occurrences of pat replaced by repl.