Skip to content

Commit d516ce8

Browse files
committed
REF: move implementation to ArrowStringArrayMixin
1 parent b3ed880 commit d516ce8

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

pandas/core/arrays/_arrow_string_mixins.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,26 @@ def _str_endswith(self, pat: str | tuple[str, ...], na: Scalar | None = None):
154154
if not isna(na): # pyright: ignore [reportGeneralTypeIssues]
155155
result = result.fill_null(na)
156156
return self._convert_bool_result(result)
157+
158+
def _str_find(self, sub: str, start: int = 0, end: int | None = None):
159+
if (start == 0 or start is None) and end is None:
160+
result = pc.find_substring(self._pa_array, sub)
161+
else:
162+
if sub == "":
163+
# GH#56792
164+
result = self._apply_elementwise(lambda val: val.find(sub, start, end))
165+
return self._convert_int_result(pa.chunked_array(result))
166+
if start is None:
167+
start_offset = 0
168+
start = 0
169+
elif start < 0:
170+
start_offset = pc.add(start, pc.utf8_length(self._pa_array))
171+
start_offset = pc.if_else(pc.less(start_offset, 0), 0, start_offset)
172+
else:
173+
start_offset = start
174+
slices = pc.utf8_slice_codeunits(self._pa_array, start, stop=end)
175+
result = pc.find_substring(slices, sub)
176+
found = pc.not_equal(result, pa.scalar(-1, type=result.type))
177+
offset_result = pc.add(result, start_offset)
178+
result = pc.if_else(found, offset_result, -1)
179+
return self._convert_int_result(result)

pandas/core/arrays/arrow/array.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2388,29 +2388,6 @@ def _str_fullmatch(
23882388
pat = f"{pat}$"
23892389
return self._str_match(pat, case, flags, na)
23902390

2391-
def _str_find(self, sub: str, start: int = 0, end: int | None = None) -> Self:
2392-
if (start == 0 or start is None) and end is None:
2393-
result = pc.find_substring(self._pa_array, sub)
2394-
else:
2395-
if sub == "":
2396-
# GH 56792
2397-
result = self._apply_elementwise(lambda val: val.find(sub, start, end))
2398-
return self._convert_int_result(pa.chunked_array(result))
2399-
if start is None:
2400-
start_offset = 0
2401-
start = 0
2402-
elif start < 0:
2403-
start_offset = pc.add(start, pc.utf8_length(self._pa_array))
2404-
start_offset = pc.if_else(pc.less(start_offset, 0), 0, start_offset)
2405-
else:
2406-
start_offset = start
2407-
slices = pc.utf8_slice_codeunits(self._pa_array, start, stop=end)
2408-
result = pc.find_substring(slices, sub)
2409-
found = pc.not_equal(result, pa.scalar(-1, type=result.type))
2410-
offset_result = pc.add(result, start_offset)
2411-
result = pc.if_else(found, offset_result, -1)
2412-
return self._convert_int_result(result)
2413-
24142391
def _str_join(self, sep: str) -> Self:
24152392
if pa.types.is_string(self._pa_array.type) or pa.types.is_large_string(
24162393
self._pa_array.type

pandas/core/arrays/string_arrow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ def _str_find(self, sub: str, start: int = 0, end: int | None = None):
456456
):
457457
# https://github.com/pandas-dev/pandas/pull/59562/files#r1725688888
458458
return super()._str_find(sub, start, end)
459-
return ArrowExtensionArray._str_find(self, sub, start, end)
459+
return ArrowStringArrayMixin._str_find(self, sub, start, end)
460460

461461
def _str_get_dummies(self, sep: str = "|"):
462462
dummies_pa, labels = ArrowExtensionArray(self._pa_array)._str_get_dummies(sep)

0 commit comments

Comments
 (0)