Skip to content

Commit 839f8d2

Browse files
committed
REF: move implementations to mixing class
1 parent fa3d536 commit 839f8d2

File tree

3 files changed

+56
-82
lines changed

3 files changed

+56
-82
lines changed

pandas/core/arrays/_arrow_string_mixins.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,37 @@ def _convert_int_result(self, result):
4242
# Convert an integer-dtype result to the appropriate result type
4343
raise NotImplementedError
4444

45+
def _str_len(self):
46+
result = pc.utf8_length(self._pa_array)
47+
return self._convert_int_result(result)
48+
49+
def _str_lower(self) -> Self:
50+
return type(self)(pc.utf8_lower(self._pa_array))
51+
52+
def _str_upper(self) -> Self:
53+
return type(self)(pc.utf8_upper(self._pa_array))
54+
55+
def _str_strip(self, to_strip=None) -> Self:
56+
if to_strip is None:
57+
result = pc.utf8_trim_whitespace(self._pa_array)
58+
else:
59+
result = pc.utf8_trim(self._pa_array, characters=to_strip)
60+
return type(self)(result)
61+
62+
def _str_lstrip(self, to_strip=None) -> Self:
63+
if to_strip is None:
64+
result = pc.utf8_ltrim_whitespace(self._pa_array)
65+
else:
66+
result = pc.utf8_ltrim(self._pa_array, characters=to_strip)
67+
return type(self)(result)
68+
69+
def _str_rstrip(self, to_strip=None) -> Self:
70+
if to_strip is None:
71+
result = pc.utf8_rtrim_whitespace(self._pa_array)
72+
else:
73+
result = pc.utf8_rtrim(self._pa_array, characters=to_strip)
74+
return type(self)(result)
75+
4576
def _str_pad(
4677
self,
4778
width: int,
@@ -205,3 +236,17 @@ def _str_contains(
205236
if not isna(na): # pyright: ignore [reportGeneralTypeIssues]
206237
result = result.fill_null(na)
207238
return self._convert_bool_result(result)
239+
240+
def _str_match(
241+
self, pat: str, case: bool = True, flags: int = 0, na: Scalar | None = None
242+
):
243+
if not pat.startswith("^"):
244+
pat = f"^{pat}"
245+
return self._str_contains(pat, case, flags, na, regex=True)
246+
247+
def _str_fullmatch(
248+
self, pat, case: bool = True, flags: int = 0, na: Scalar | None = None
249+
):
250+
if not pat.endswith("$") or pat.endswith("\\$"):
251+
pat = f"{pat}$"
252+
return self._str_match(pat, case, flags, na)

pandas/core/arrays/arrow/array.py

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1998,7 +1998,7 @@ def _rank(
19981998
"""
19991999
See Series.rank.__doc__.
20002000
"""
2001-
return type(self)(
2001+
return self._convert_int_result(
20022002
self._rank_calc(
20032003
axis=axis,
20042004
method=method,
@@ -2322,9 +2322,6 @@ def _str_count(self, pat: str, flags: int = 0) -> Self:
23222322
raise NotImplementedError(f"count not implemented with {flags=}")
23232323
return type(self)(pc.count_substring_regex(self._pa_array, pat))
23242324

2325-
def _result_converter(self, result):
2326-
return type(self)(result)
2327-
23282325
def _str_replace(
23292326
self,
23302327
pat: str | re.Pattern,
@@ -2359,20 +2356,6 @@ def _str_repeat(self, repeats: int | Sequence[int]) -> Self:
23592356
)
23602357
return type(self)(pc.binary_repeat(self._pa_array, repeats))
23612358

2362-
def _str_match(
2363-
self, pat: str, case: bool = True, flags: int = 0, na: Scalar | None = None
2364-
) -> Self:
2365-
if not pat.startswith("^"):
2366-
pat = f"^{pat}"
2367-
return self._str_contains(pat, case, flags, na, regex=True)
2368-
2369-
def _str_fullmatch(
2370-
self, pat, case: bool = True, flags: int = 0, na: Scalar | None = None
2371-
) -> Self:
2372-
if not pat.endswith("$") or pat.endswith("\\$"):
2373-
pat = f"{pat}$"
2374-
return self._str_match(pat, case, flags, na)
2375-
23762359
def _str_find(self, sub: str, start: int = 0, end: int | None = None) -> Self:
23772360
if (start == 0 or start is None) and end is None:
23782361
result = pc.find_substring(self._pa_array, sub)
@@ -2427,36 +2410,6 @@ def _str_slice(
24272410
pc.utf8_slice_codeunits(self._pa_array, start=start, stop=stop, step=step)
24282411
)
24292412

2430-
def _str_len(self) -> Self:
2431-
return type(self)(pc.utf8_length(self._pa_array))
2432-
2433-
def _str_lower(self) -> Self:
2434-
return type(self)(pc.utf8_lower(self._pa_array))
2435-
2436-
def _str_upper(self) -> Self:
2437-
return type(self)(pc.utf8_upper(self._pa_array))
2438-
2439-
def _str_strip(self, to_strip=None) -> Self:
2440-
if to_strip is None:
2441-
result = pc.utf8_trim_whitespace(self._pa_array)
2442-
else:
2443-
result = pc.utf8_trim(self._pa_array, characters=to_strip)
2444-
return type(self)(result)
2445-
2446-
def _str_lstrip(self, to_strip=None) -> Self:
2447-
if to_strip is None:
2448-
result = pc.utf8_ltrim_whitespace(self._pa_array)
2449-
else:
2450-
result = pc.utf8_ltrim(self._pa_array, characters=to_strip)
2451-
return type(self)(result)
2452-
2453-
def _str_rstrip(self, to_strip=None) -> Self:
2454-
if to_strip is None:
2455-
result = pc.utf8_rtrim_whitespace(self._pa_array)
2456-
else:
2457-
result = pc.utf8_rtrim(self._pa_array, characters=to_strip)
2458-
return type(self)(result)
2459-
24602413
def _str_removeprefix(self, prefix: str):
24612414
if not pa_version_under13p0:
24622415
starts_with = pc.starts_with(self._pa_array, pattern=prefix)

pandas/core/arrays/string_arrow.py

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454

5555
from pandas._typing import (
5656
ArrayLike,
57-
AxisInt,
5857
Dtype,
5958
Self,
6059
npt,
@@ -292,19 +291,22 @@ def astype(self, dtype, copy: bool = True):
292291
_str_startswith = ArrowStringArrayMixin._str_startswith
293292
_str_endswith = ArrowStringArrayMixin._str_endswith
294293
_str_pad = ArrowStringArrayMixin._str_pad
295-
_str_match = ArrowExtensionArray._str_match
296-
_str_fullmatch = ArrowExtensionArray._str_fullmatch
297-
_str_lower = ArrowExtensionArray._str_lower
298-
_str_upper = ArrowExtensionArray._str_upper
299-
_str_strip = ArrowExtensionArray._str_strip
300-
_str_lstrip = ArrowExtensionArray._str_lstrip
301-
_str_rstrip = ArrowExtensionArray._str_rstrip
294+
_str_match = ArrowStringArrayMixin._str_match
295+
_str_fullmatch = ArrowStringArrayMixin._str_fullmatch
296+
_str_lower = ArrowStringArrayMixin._str_lower
297+
_str_upper = ArrowStringArrayMixin._str_upper
298+
_str_strip = ArrowStringArrayMixin._str_strip
299+
_str_lstrip = ArrowStringArrayMixin._str_lstrip
300+
_str_rstrip = ArrowStringArrayMixin._str_rstrip
302301
_str_removesuffix = ArrowStringArrayMixin._str_removesuffix
303302
_str_get = ArrowStringArrayMixin._str_get
304303
_str_capitalize = ArrowStringArrayMixin._str_capitalize
305304
_str_title = ArrowStringArrayMixin._str_title
306305
_str_swapcase = ArrowStringArrayMixin._str_swapcase
307306
_str_slice_replace = ArrowStringArrayMixin._str_slice_replace
307+
_str_len = ArrowStringArrayMixin._str_len
308+
309+
_rank = ArrowExtensionArray._rank
308310

309311
def _str_contains(
310312
self, pat, case: bool = True, flags: int = 0, na=np.nan, regex: bool = True
@@ -356,10 +358,6 @@ def _str_slice(
356358
return super()._str_slice(start, stop, step)
357359
return ArrowExtensionArray._str_slice(self, start=start, stop=stop, step=step)
358360

359-
def _str_len(self):
360-
result = pc.utf8_length(self._pa_array)
361-
return self._convert_int_result(result)
362-
363361
def _str_removeprefix(self, prefix: str):
364362
if not pa_version_under13p0:
365363
return ArrowExtensionArray._str_removeprefix(self, prefix)
@@ -425,28 +423,6 @@ def _reduce(
425423
else:
426424
return result
427425

428-
def _rank(
429-
self,
430-
*,
431-
axis: AxisInt = 0,
432-
method: str = "average",
433-
na_option: str = "keep",
434-
ascending: bool = True,
435-
pct: bool = False,
436-
):
437-
"""
438-
See Series.rank.__doc__.
439-
"""
440-
return self._convert_int_result(
441-
self._rank_calc(
442-
axis=axis,
443-
method=method,
444-
na_option=na_option,
445-
ascending=ascending,
446-
pct=pct,
447-
)
448-
)
449-
450426
def value_counts(self, dropna: bool = True) -> Series:
451427
result = super().value_counts(dropna=dropna)
452428
if self.dtype.na_value is np.nan:

0 commit comments

Comments
 (0)