Skip to content

Commit c913763

Browse files
committed
REF: move implementation to ArrowStringArrayMixin
1 parent 8545244 commit c913763

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
@@ -190,3 +190,26 @@ def _str_istitle(self):
190190
def _str_isupper(self):
191191
result = pc.utf8_is_upper(self._pa_array)
192192
return self._convert_bool_result(result)
193+
194+
def _str_find(self, sub: str, start: int = 0, end: int | None = None):
195+
if (start == 0 or start is None) and end is None:
196+
result = pc.find_substring(self._pa_array, sub)
197+
else:
198+
if sub == "":
199+
# GH#56792
200+
result = self._apply_elementwise(lambda val: val.find(sub, start, end))
201+
return self._convert_int_result(pa.chunked_array(result))
202+
if start is None:
203+
start_offset = 0
204+
start = 0
205+
elif start < 0:
206+
start_offset = pc.add(start, pc.utf8_length(self._pa_array))
207+
start_offset = pc.if_else(pc.less(start_offset, 0), 0, start_offset)
208+
else:
209+
start_offset = start
210+
slices = pc.utf8_slice_codeunits(self._pa_array, start, stop=end)
211+
result = pc.find_substring(slices, sub)
212+
found = pc.not_equal(result, pa.scalar(-1, type=result.type))
213+
offset_result = pc.add(result, start_offset)
214+
result = pc.if_else(found, offset_result, -1)
215+
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
@@ -430,7 +430,7 @@ def _str_find(self, sub: str, start: int = 0, end: int | None = None):
430430
):
431431
# https://github.com/pandas-dev/pandas/pull/59562/files#r1725688888
432432
return super()._str_find(sub, start, end)
433-
return ArrowExtensionArray._str_find(self, sub, start, end)
433+
return ArrowStringArrayMixin._str_find(self, sub, start, end)
434434

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

0 commit comments

Comments
 (0)