MCPcopy
hub / github.com/pydata/xarray / replace

Method replace

xarray/core/accessor_str.py:1882–1953  ·  view source on GitHub ↗

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,
    )

Source from the content-addressed store, hash-verified

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"

Callers 15

decode_cf_variablesFunction · 0.80
_parse_iso8601Function · 0.80
_shift_monthFunction · 0.80
normalize_dateFunction · 0.80
_new_to_legacy_freqFunction · 0.80
_legacy_to_new_freqFunction · 0.80
date_range_likeFunction · 0.80
__repr__Method · 0.80
_update_doc_to_datasetFunction · 0.80

Calls 6

_stringifyMethod · 0.95
_re_compileMethod · 0.95
_applyMethod · 0.95
_contains_str_likeFunction · 0.85
_contains_callableFunction · 0.85
_contains_compiled_reFunction · 0.85

Tested by 11

test_write_emptyMethod · 0.64
test_batchdap4_downloadsFunction · 0.64
test_diff_array_reprMethod · 0.64
test_display_nbytesFunction · 0.64
test_replaceFunction · 0.64
test_replace_callableFunction · 0.64
test_replace_unicodeFunction · 0.64
test_replace_literalFunction · 0.64
test_empty_str_methodsFunction · 0.64