Skip to content

Commit 50604dc

Browse files
simonjayhawkinsTLouf
authored andcommitted
[ArrowStringArray] PERF: use pyarrow.compute.replace_substring(_regex) if available (pandas-dev#41590)
1 parent c5fe0e5 commit 50604dc

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

pandas/core/arrays/string_arrow.py

+23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
from collections.abc import Callable # noqa: PDF001
34
import re
45
from typing import (
56
TYPE_CHECKING,
@@ -834,6 +835,28 @@ def _str_endswith(self, pat: str, na=None):
834835
pat = re.escape(pat) + "$"
835836
return self._str_contains(pat, na=na, regex=True)
836837

838+
def _str_replace(
839+
self,
840+
pat: str | re.Pattern,
841+
repl: str | Callable,
842+
n: int = -1,
843+
case: bool = True,
844+
flags: int = 0,
845+
regex: bool = True,
846+
):
847+
if (
848+
pa_version_under4p0
849+
or isinstance(pat, re.Pattern)
850+
or callable(repl)
851+
or not case
852+
or flags
853+
):
854+
return super()._str_replace(pat, repl, n, case, flags, regex)
855+
856+
func = pc.replace_substring_regex if regex else pc.replace_substring
857+
result = func(self._data, pattern=pat, replacement=repl, max_replacements=n)
858+
return type(self)(result)
859+
837860
def _str_match(
838861
self, pat: str, case: bool = True, flags: int = 0, na: Scalar = None
839862
):

0 commit comments

Comments
 (0)