r""" Replace each occurrence of pattern/regex in the Series/Index. Equivalent to :meth:`str.replace` or :func:`re.sub`, depending on the regex value. Parameters ---------- pat : str, compiled regex, or a dict String can be a character seq
(
self,
pat: str | re.Pattern | dict,
repl: str | Callable | None = None,
n: int = -1,
case: bool | None = None,
flags: int = 0,
regex: bool = False,
)
| 1636 | |
| 1637 | @forbid_nonstring_types(["bytes"]) |
| 1638 | def replace( |
| 1639 | self, |
| 1640 | pat: str | re.Pattern | dict, |
| 1641 | repl: str | Callable | None = None, |
| 1642 | n: int = -1, |
| 1643 | case: bool | None = None, |
| 1644 | flags: int = 0, |
| 1645 | regex: bool = False, |
| 1646 | ): |
| 1647 | r""" |
| 1648 | Replace each occurrence of pattern/regex in the Series/Index. |
| 1649 | |
| 1650 | Equivalent to :meth:`str.replace` or :func:`re.sub`, depending on |
| 1651 | the regex value. |
| 1652 | |
| 1653 | Parameters |
| 1654 | ---------- |
| 1655 | pat : str, compiled regex, or a dict |
| 1656 | String can be a character sequence or regular expression. |
| 1657 | Dictionary contains <key : value> pairs of strings to be replaced |
| 1658 | along with the updated value. |
| 1659 | repl : str or callable |
| 1660 | Replacement string or a callable. The callable is passed the regex |
| 1661 | match object and must return a replacement string to be used. |
| 1662 | Must have a value of None if `pat` is a dict |
| 1663 | See :func:`re.sub`. |
| 1664 | n : int, default -1 (all) |
| 1665 | Number of replacements to make from start. |
| 1666 | case : bool, default None |
| 1667 | Determines if replace is case sensitive: |
| 1668 | |
| 1669 | - If True, case sensitive (the default if `pat` is a string) |
| 1670 | - Set to False for case insensitive |
| 1671 | - Cannot be set if `pat` is a compiled regex. |
| 1672 | |
| 1673 | flags : int, default 0 (no flags) |
| 1674 | Regex module flags, e.g. re.IGNORECASE. Cannot be set if `pat` is a compiled |
| 1675 | regex. |
| 1676 | regex : bool, default False |
| 1677 | Determines if the passed-in pattern is a regular expression: |
| 1678 | |
| 1679 | - If True, assumes the passed-in pattern is a regular expression. |
| 1680 | - If False, treats the pattern as a literal string |
| 1681 | - Cannot be set to False if `pat` is a compiled regex or `repl` is |
| 1682 | a callable. |
| 1683 | |
| 1684 | Returns |
| 1685 | ------- |
| 1686 | Series or Index of object |
| 1687 | A copy of the object with all matching occurrences of `pat` replaced by |
| 1688 | `repl`. |
| 1689 | |
| 1690 | Raises |
| 1691 | ------ |
| 1692 | ValueError |
| 1693 | * if `regex` is False and `repl` is a callable or `pat` is a compiled |
| 1694 | regex |
| 1695 | * if `pat` is a compiled regex and `case` or `flags` is set |
no test coverage detected