def should_use_regex(regex: bool, to_replace: Any) -> bool: """ Decide whether to treat `to_replace` as a regular expression. """ if is_re(to_replace): regex = True regex = regex and is_re_compilable(to_replace) # Don't use regex if the pattern is empty. regex = regex and re.compile(to_replace).pattern != "" return regex
return self._str_map(f, na, dtype=np.dtype("bool")) def _str_startswith(self, pat, na=None): f = lambda x: x.startswith(pat) return self._str_map(f, na_value=na, dtype=np.dtype(bool)) def _str_endswith(self, pat, na=None): f = lambda x: x.endswith(pat) return self._str_map(f, na_value=na, dtype=np.dtype(bool)) def _str_replace(self, pat, repl, n=-1, case=None, flags=0, regex=True): # Check whether repl is valid (GH 13438, GH 15055) if not (isinstance(repl, str) or callable(repl)): raise TypeError("repl must be a string or callable") is_compiled_re = is_re(pat) if regex: if is_compiled_re: if (case is not None) or (flags != 0): raise ValueError( "case and flags cannot be set when pat is a compiled regex" ) else: # not a compiled regex # set default case if case is None: case = True # add case flag, if provided if case is False: flags |= re.IGNORECASE
def re_replacer(s): if is_re(rx) and isinstance(s, str): return rx.sub(value, s) else: return s
def re_replacer(s): if is_re(rx) and isinstance(s, str): return value if rx.search(s) is not None else s else: return s