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.Patter
(
self,
pat: str | bytes | Pattern | Any,
repl: str | bytes | Callable | Any,
n: int | Any = -1,
case: bool | None = None,
flags: int = 0,
regex: bool = True,
)
| 1880 | return self.index(sub, start=start, end=end, side="right") |
| 1881 | |
| 1882 | def replace( |
| 1883 | self, |
| 1884 | pat: str | bytes | Pattern | Any, |
| 1885 | repl: str | bytes | Callable | Any, |
| 1886 | n: int | Any = -1, |
| 1887 | case: bool | None = None, |
| 1888 | flags: int = 0, |
| 1889 | regex: bool = True, |
| 1890 | ) -> T_DataArray: |
| 1891 | """ |
| 1892 | Replace occurrences of pattern/regex in the array with some string. |
| 1893 | |
| 1894 | If `pat`, `repl`, or 'n` is array-like, they are broadcast |
| 1895 | against the array and applied elementwise. |
| 1896 | |
| 1897 | Parameters |
| 1898 | ---------- |
| 1899 | pat : str or re.Pattern or array-like of str or re.Pattern |
| 1900 | String can be a character sequence or regular expression. |
| 1901 | If array-like, it is broadcast. |
| 1902 | repl : str or callable or array-like of str or callable |
| 1903 | Replacement string or a callable. The callable is passed the regex |
| 1904 | match object and must return a replacement string to be used. |
| 1905 | See :func:`re.sub`. |
| 1906 | If array-like, it is broadcast. |
| 1907 | n : int or array of int, default: -1 |
| 1908 | Number of replacements to make from start. Use ``-1`` to replace all. |
| 1909 | If array-like, it is broadcast. |
| 1910 | case : bool, default: True |
| 1911 | If True, case sensitive. |
| 1912 | Cannot be set if `pat` is a compiled regex. |
| 1913 | Equivalent to setting the `re.IGNORECASE` flag. |
| 1914 | flags : int, default: 0 |
| 1915 | Flags to pass through to the re module, e.g. `re.IGNORECASE`. |
| 1916 | see `compilation-flags <https://docs.python.org/3/howto/regex.html#compilation-flags>`_. |
| 1917 | ``0`` means no flags. Flags can be combined with the bitwise or operator ``|``. |
| 1918 | Cannot be set if `pat` is a compiled regex. |
| 1919 | regex : bool, default: True |
| 1920 | If True, assumes the passed-in pattern is a regular expression. |
| 1921 | If False, treats the pattern as a literal string. |
| 1922 | Cannot be set to False if `pat` is a compiled regex or `repl` is |
| 1923 | a callable. |
| 1924 | |
| 1925 | Returns |
| 1926 | ------- |
| 1927 | replaced : same type as values |
| 1928 | A copy of the object with all matching occurrences of `pat` |
| 1929 | replaced by `repl`. |
| 1930 | """ |
| 1931 | if _contains_str_like(repl): |
| 1932 | repl = self._stringify(repl) |
| 1933 | elif not _contains_callable(repl): # pragma: no cover |
| 1934 | raise TypeError("repl must be a string or callable") |
| 1935 | |
| 1936 | is_compiled_re = _contains_compiled_re(pat) |
| 1937 | if not regex and is_compiled_re: |
| 1938 | raise ValueError( |
| 1939 | "Cannot use a compiled regex as replacement pattern with regex=False" |