{"record":{"id":"319db337ada2d300","repo":"pandas-dev/pandas","slug":"replace-is-not-supported-with-a-re-pattern-callab","errorCode":null,"errorMessage":"replace is not supported with a re.Pattern, callable repl, case=False, flags!=0, or when the replacement string contains named group references (\\g<...>)","messagePattern":"replace is not supported with a re\\.Pattern, callable repl, case=False, flags!=0, or when the replacement string contains named group references \\(\\\\g<\\.\\.\\.>\\)","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/_arrow_string_mixins.py","lineNumber":262,"sourceCode":"        )\n\n    def _str_replace(\n        self,\n        pat: str | re.Pattern,\n        repl: str | Callable,\n        n: int = -1,\n        case: bool = True,\n        flags: int = 0,\n        regex: bool = True,\n    ) -> Self:\n        if (\n            isinstance(pat, re.Pattern)\n            or callable(repl)\n            or not case\n            or flags\n            or (isinstance(repl, str) and r\"\\g<\" in repl)\n        ):\n            raise NotImplementedError(\n                \"replace is not supported with a re.Pattern, callable repl, \"\n                \"case=False, flags!=0, or when the replacement string contains \"\n                \"named group references (\\\\g<...>)\"\n            )\n\n        if pat == \"\":\n            # pyarrow hangs for empty patterns\n            # (https://github.com/apache/arrow/issues/39149)\n            # use same func definition as ObjectStringArrayMixin._str_replace\n            if regex:\n                count = n if n >= 0 else 0\n                func = lambda val: re.sub(pat, repl, val, count=count)\n            else:\n                func = lambda val: val.replace(pat, repl, n)\n\n            result = self._apply_elementwise(func)\n            return self._from_pyarrow_array(\n                pa.chunked_array(result, type=self._pa_array.type)","sourceCodeStart":244,"sourceCodeEnd":280,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/_arrow_string_mixins.py#L244-L280","documentation":"Series.str.replace on the pyarrow backend cannot express every feature of Python's re module. It raises NotImplementedError when pat is a compiled re.Pattern, repl is callable, case=False, flags is non-zero, or the replacement string contains a named-group reference \\g<...>. pyarrow's replace_substring(_regex) kernels have no equivalent for those, so pandas refuses rather than silently producing wrong results.","triggerScenarios":"On a string[pyarrow] Series: s.str.replace(r'\\d+', lambda m: ..., regex=True); s.str.replace(pat, repl, flags=re.IGNORECASE); s.str.replace(compiled_re, 'x'); s.str.replace('a', r'\\g<name>'); or s.str.replace('a','b', case=False).","commonSituations":"Migrating object/string-dtype code that relies on callable replacements or re flags to pyarrow dtypes; using named-group backreferences in replacement templates.","solutions":["Convert to object dtype first: s.astype(object).str.replace(...) recovers full re semantics.","Drop the unsupported feature: use a plain string pattern, a string repl, default case/flags, and numeric group refs like \\1 instead of \\g<name>.","Move callable-replacement logic out of str.replace into an apply/elementwise step."],"exampleFix":"// before\ns.str.replace(r\"\\d+\", lambda m: f\"[{m.group()}]\", regex=True)\n// after\ns.astype(object).str.replace(r\"\\d+\", lambda m: f\"[{m.group()}]\", regex=True)","handlingStrategy":"fallback","validationCode":"def pyarrow_replace(s, pat, repl, **kw):\n    needs_object = (\n        isinstance(pat, re.Pattern)\n        or callable(repl)\n        or not kw.get(\"case\", True)\n        or kw.get(\"flags\", 0)\n        or (isinstance(repl, str) and r\"\\g<\" in repl)\n    )\n    if needs_object and \"string[pyarrow]\" in str(s.dtype):\n        s = s.astype(object)\n    return s.str.replace(pat, repl, **kw)","typeGuard":null,"tryCatchPattern":"try:\n    out = s.str.replace(pat, repl, regex=True)\nexcept NotImplementedError:\n    out = s.astype(object).str.replace(pat, repl, regex=True)","preventionTips":["Avoid callable repl / re flags / named-group refs on pyarrow string Series","Fall back to object dtype when full re semantics are required"],"tags":["pyarrow","string","regex","not-implemented","compatibility"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}