Skip to content

Commit c083ed0

Browse files
jbrockmendeljorisvandenbossche
authored andcommitted
REF (string): de-duplicate str_isfoo methods (#59705)
1 parent bb7e65c commit c083ed0

File tree

3 files changed

+48
-65
lines changed

3 files changed

+48
-65
lines changed

pandas/core/arrays/_arrow_string_mixins.py

+38-2
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ def _str_pad(
5252
elif side == "both":
5353
if pa_version_under17p0:
5454
# GH#59624 fall back to object dtype
55-
from pandas import array
55+
from pandas import array as pd_array
5656

5757
obj_arr = self.astype(object, copy=False) # type: ignore[attr-defined]
58-
obj = array(obj_arr, dtype=object)
58+
obj = pd_array(obj_arr, dtype=object)
5959
result = obj._str_pad(width, side, fillchar) # type: ignore[attr-defined]
6060
return type(self)._from_sequence(result, dtype=self.dtype) # type: ignore[attr-defined]
6161
else:
@@ -150,3 +150,39 @@ def _str_endswith(self, pat: str | tuple[str, ...], na: Scalar | None = None):
150150
if not isna(na): # pyright: ignore [reportGeneralTypeIssues]
151151
result = result.fill_null(na)
152152
return self._convert_bool_result(result)
153+
154+
def _str_isalnum(self):
155+
result = pc.utf8_is_alnum(self._pa_array)
156+
return self._convert_bool_result(result)
157+
158+
def _str_isalpha(self):
159+
result = pc.utf8_is_alpha(self._pa_array)
160+
return self._convert_bool_result(result)
161+
162+
def _str_isdecimal(self):
163+
result = pc.utf8_is_decimal(self._pa_array)
164+
return self._convert_bool_result(result)
165+
166+
def _str_isdigit(self):
167+
result = pc.utf8_is_digit(self._pa_array)
168+
return self._convert_bool_result(result)
169+
170+
def _str_islower(self):
171+
result = pc.utf8_is_lower(self._pa_array)
172+
return self._convert_bool_result(result)
173+
174+
def _str_isnumeric(self):
175+
result = pc.utf8_is_numeric(self._pa_array)
176+
return self._convert_bool_result(result)
177+
178+
def _str_isspace(self):
179+
result = pc.utf8_is_space(self._pa_array)
180+
return self._convert_bool_result(result)
181+
182+
def _str_istitle(self):
183+
result = pc.utf8_is_title(self._pa_array)
184+
return self._convert_bool_result(result)
185+
186+
def _str_isupper(self):
187+
result = pc.utf8_is_upper(self._pa_array)
188+
return self._convert_bool_result(result)

pandas/core/arrays/arrow/array.py

-27
Original file line numberDiff line numberDiff line change
@@ -2411,33 +2411,6 @@ def _str_slice(
24112411
pc.utf8_slice_codeunits(self._pa_array, start=start, stop=stop, step=step)
24122412
)
24132413

2414-
def _str_isalnum(self):
2415-
return type(self)(pc.utf8_is_alnum(self._pa_array))
2416-
2417-
def _str_isalpha(self):
2418-
return type(self)(pc.utf8_is_alpha(self._pa_array))
2419-
2420-
def _str_isdecimal(self):
2421-
return type(self)(pc.utf8_is_decimal(self._pa_array))
2422-
2423-
def _str_isdigit(self):
2424-
return type(self)(pc.utf8_is_digit(self._pa_array))
2425-
2426-
def _str_islower(self):
2427-
return type(self)(pc.utf8_is_lower(self._pa_array))
2428-
2429-
def _str_isnumeric(self):
2430-
return type(self)(pc.utf8_is_numeric(self._pa_array))
2431-
2432-
def _str_isspace(self):
2433-
return type(self)(pc.utf8_is_space(self._pa_array))
2434-
2435-
def _str_istitle(self):
2436-
return type(self)(pc.utf8_is_title(self._pa_array))
2437-
2438-
def _str_isupper(self):
2439-
return type(self)(pc.utf8_is_upper(self._pa_array))
2440-
24412414
def _str_len(self):
24422415
return type(self)(pc.utf8_length(self._pa_array))
24432416

pandas/core/arrays/string_arrow.py

+10-36
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,16 @@ def _data(self):
283283
# ------------------------------------------------------------------------
284284
# String methods interface
285285

286+
_str_isalnum = ArrowStringArrayMixin._str_isalnum
287+
_str_isalpha = ArrowStringArrayMixin._str_isalpha
288+
_str_isdecimal = ArrowStringArrayMixin._str_isdecimal
289+
_str_isdigit = ArrowStringArrayMixin._str_isdigit
290+
_str_islower = ArrowStringArrayMixin._str_islower
291+
_str_isnumeric = ArrowStringArrayMixin._str_isnumeric
292+
_str_isspace = ArrowStringArrayMixin._str_isspace
293+
_str_istitle = ArrowStringArrayMixin._str_istitle
294+
_str_isupper = ArrowStringArrayMixin._str_isupper
295+
286296
_str_map = BaseStringArray._str_map
287297
_str_startswith = ArrowStringArrayMixin._str_startswith
288298
_str_endswith = ArrowStringArrayMixin._str_endswith
@@ -360,42 +370,6 @@ def _str_slice(
360370
pc.utf8_slice_codeunits(self._pa_array, start=start, stop=stop, step=step)
361371
)
362372

363-
def _str_isalnum(self):
364-
result = pc.utf8_is_alnum(self._pa_array)
365-
return self._convert_bool_result(result)
366-
367-
def _str_isalpha(self):
368-
result = pc.utf8_is_alpha(self._pa_array)
369-
return self._convert_bool_result(result)
370-
371-
def _str_isdecimal(self):
372-
result = pc.utf8_is_decimal(self._pa_array)
373-
return self._convert_bool_result(result)
374-
375-
def _str_isdigit(self):
376-
result = pc.utf8_is_digit(self._pa_array)
377-
return self._convert_bool_result(result)
378-
379-
def _str_islower(self):
380-
result = pc.utf8_is_lower(self._pa_array)
381-
return self._convert_bool_result(result)
382-
383-
def _str_isnumeric(self):
384-
result = pc.utf8_is_numeric(self._pa_array)
385-
return self._convert_bool_result(result)
386-
387-
def _str_isspace(self):
388-
result = pc.utf8_is_space(self._pa_array)
389-
return self._convert_bool_result(result)
390-
391-
def _str_istitle(self):
392-
result = pc.utf8_is_title(self._pa_array)
393-
return self._convert_bool_result(result)
394-
395-
def _str_isupper(self):
396-
result = pc.utf8_is_upper(self._pa_array)
397-
return self._convert_bool_result(result)
398-
399373
def _str_len(self):
400374
result = pc.utf8_length(self._pa_array)
401375
return self._convert_int_result(result)

0 commit comments

Comments
 (0)