Skip to content

Commit f11921e

Browse files
committed
REF: move implementation to ArrowStringArrayMixin
1 parent d9f0aa7 commit f11921e

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
@@ -205,3 +205,26 @@ def _str_contains(
205205
if not isna(na): # pyright: ignore [reportGeneralTypeIssues]
206206
result = result.fill_null(na)
207207
return self._convert_bool_result(result)
208+
209+
def _str_find(self, sub: str, start: int = 0, end: int | None = None):
210+
if (start == 0 or start is None) and end is None:
211+
result = pc.find_substring(self._pa_array, sub)
212+
else:
213+
if sub == "":
214+
# GH#56792
215+
result = self._apply_elementwise(lambda val: val.find(sub, start, end))
216+
return self._convert_int_result(pa.chunked_array(result))
217+
if start is None:
218+
start_offset = 0
219+
start = 0
220+
elif start < 0:
221+
start_offset = pc.add(start, pc.utf8_length(self._pa_array))
222+
start_offset = pc.if_else(pc.less(start_offset, 0), 0, start_offset)
223+
else:
224+
start_offset = start
225+
slices = pc.utf8_slice_codeunits(self._pa_array, start, stop=end)
226+
result = pc.find_substring(slices, sub)
227+
found = pc.not_equal(result, pa.scalar(-1, type=result.type))
228+
offset_result = pc.add(result, start_offset)
229+
result = pc.if_else(found, offset_result, -1)
230+
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
@@ -2373,29 +2373,6 @@ def _str_fullmatch(
23732373
pat = f"{pat}$"
23742374
return self._str_match(pat, case, flags, na)
23752375

2376-
def _str_find(self, sub: str, start: int = 0, end: int | None = None) -> Self:
2377-
if (start == 0 or start is None) and end is None:
2378-
result = pc.find_substring(self._pa_array, sub)
2379-
else:
2380-
if sub == "":
2381-
# GH 56792
2382-
result = self._apply_elementwise(lambda val: val.find(sub, start, end))
2383-
return self._convert_int_result(pa.chunked_array(result))
2384-
if start is None:
2385-
start_offset = 0
2386-
start = 0
2387-
elif start < 0:
2388-
start_offset = pc.add(start, pc.utf8_length(self._pa_array))
2389-
start_offset = pc.if_else(pc.less(start_offset, 0), 0, start_offset)
2390-
else:
2391-
start_offset = start
2392-
slices = pc.utf8_slice_codeunits(self._pa_array, start, stop=end)
2393-
result = pc.find_substring(slices, sub)
2394-
found = pc.not_equal(result, pa.scalar(-1, type=result.type))
2395-
offset_result = pc.add(result, start_offset)
2396-
result = pc.if_else(found, offset_result, -1)
2397-
return self._convert_int_result(result)
2398-
23992376
def _str_join(self, sep: str) -> Self:
24002377
if pa.types.is_string(self._pa_array.type) or pa.types.is_large_string(
24012378
self._pa_array.type

pandas/core/arrays/string_arrow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ def _str_find(self, sub: str, start: int = 0, end: int | None = None):
424424
):
425425
# https://github.com/pandas-dev/pandas/pull/59562/files#r1725688888
426426
return super()._str_find(sub, start, end)
427-
return ArrowExtensionArray._str_find(self, sub, start, end)
427+
return ArrowStringArrayMixin._str_find(self, sub, start, end)
428428

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

0 commit comments

Comments
 (0)