1from __future__ import annotations
2
3import abc
4import re
5from typing import (
6 TYPE_CHECKING,
7 Callable,
8 Literal,
9)
10
11import numpy as np
12
13from pandas._typing import Scalar
14
15if TYPE_CHECKING:
16 from pandas import Series
17
18
19class BaseStringArrayMethods(abc.ABC):
20 """
21 Base class for extension arrays implementing string methods.
22
23 This is where our ExtensionArrays can override the implementation of
24 Series.str.<method>. We don't expect this to work with
25 3rd-party extension arrays.
26
27 * User calls Series.str.<method>
28 * pandas extracts the extension array from the Series
29 * pandas calls ``extension_array._str_<method>(*args, **kwargs)``
30 * pandas wraps the result, to return to the user.
31
32 See :ref:`Series.str` for the docstring of each method.
33 """
34
35 def _str_getitem(self, key):
36 if isinstance(key, slice):
37 return self._str_slice(start=key.start, stop=key.stop, step=key.step)
38 else:
39 return self._str_get(key)
40
41 @abc.abstractmethod
42 def _str_count(self, pat, flags: int = 0):
43 pass
44
45 @abc.abstractmethod
46 def _str_pad(
47 self,
48 width,
49 side: Literal["left", "right", "both"] = "left",
50 fillchar: str = " ",
51 ):
52 pass
53
54 @abc.abstractmethod
55 def _str_contains(
56 self, pat, case: bool = True, flags: int = 0, na=None, regex: bool = True
57 ):
58 pass
59
60 @abc.abstractmethod
61 def _str_startswith(self, pat, na=None):
62 pass
63
64 @abc.abstractmethod
65 def _str_endswith(self, pat, na=None):
66 pass
67
68 @abc.abstractmethod
69 def _str_replace(
70 self,
71 pat: str | re.Pattern,
72 repl: str | Callable,
73 n: int = -1,
74 case: bool = True,
75 flags: int = 0,
76 regex: bool = True,
77 ):
78 pass
79
80 @abc.abstractmethod
81 def _str_repeat(self, repeats):
82 pass
83
84 @abc.abstractmethod
85 def _str_match(
86 self, pat: str, case: bool = True, flags: int = 0, na: Scalar = np.nan
87 ):
88 pass
89
90 @abc.abstractmethod
91 def _str_fullmatch(
92 self,
93 pat: str | re.Pattern,
94 case: bool = True,
95 flags: int = 0,
96 na: Scalar = np.nan,
97 ):
98 pass
99
100 @abc.abstractmethod
101 def _str_encode(self, encoding, errors: str = "strict"):
102 pass
103
104 @abc.abstractmethod
105 def _str_find(self, sub, start: int = 0, end=None):
106 pass
107
108 @abc.abstractmethod
109 def _str_rfind(self, sub, start: int = 0, end=None):
110 pass
111
112 @abc.abstractmethod
113 def _str_findall(self, pat, flags: int = 0):
114 pass
115
116 @abc.abstractmethod
117 def _str_get(self, i):
118 pass
119
120 @abc.abstractmethod
121 def _str_index(self, sub, start: int = 0, end=None):
122 pass
123
124 @abc.abstractmethod
125 def _str_rindex(self, sub, start: int = 0, end=None):
126 pass
127
128 @abc.abstractmethod
129 def _str_join(self, sep):
130 pass
131
132 @abc.abstractmethod
133 def _str_partition(self, sep, expand):
134 pass
135
136 @abc.abstractmethod
137 def _str_rpartition(self, sep, expand):
138 pass
139
140 @abc.abstractmethod
141 def _str_len(self):
142 pass
143
144 @abc.abstractmethod
145 def _str_slice(self, start=None, stop=None, step=None):
146 pass
147
148 @abc.abstractmethod
149 def _str_slice_replace(self, start=None, stop=None, repl=None):
150 pass
151
152 @abc.abstractmethod
153 def _str_translate(self, table):
154 pass
155
156 @abc.abstractmethod
157 def _str_wrap(self, width, **kwargs):
158 pass
159
160 @abc.abstractmethod
161 def _str_get_dummies(self, sep: str = "|"):
162 pass
163
164 @abc.abstractmethod
165 def _str_isalnum(self):
166 pass
167
168 @abc.abstractmethod
169 def _str_isalpha(self):
170 pass
171
172 @abc.abstractmethod
173 def _str_isdecimal(self):
174 pass
175
176 @abc.abstractmethod
177 def _str_isdigit(self):
178 pass
179
180 @abc.abstractmethod
181 def _str_islower(self):
182 pass
183
184 @abc.abstractmethod
185 def _str_isnumeric(self):
186 pass
187
188 @abc.abstractmethod
189 def _str_isspace(self):
190 pass
191
192 @abc.abstractmethod
193 def _str_istitle(self):
194 pass
195
196 @abc.abstractmethod
197 def _str_isupper(self):
198 pass
199
200 @abc.abstractmethod
201 def _str_capitalize(self):
202 pass
203
204 @abc.abstractmethod
205 def _str_casefold(self):
206 pass
207
208 @abc.abstractmethod
209 def _str_title(self):
210 pass
211
212 @abc.abstractmethod
213 def _str_swapcase(self):
214 pass
215
216 @abc.abstractmethod
217 def _str_lower(self):
218 pass
219
220 @abc.abstractmethod
221 def _str_upper(self):
222 pass
223
224 @abc.abstractmethod
225 def _str_normalize(self, form):
226 pass
227
228 @abc.abstractmethod
229 def _str_strip(self, to_strip=None):
230 pass
231
232 @abc.abstractmethod
233 def _str_lstrip(self, to_strip=None):
234 pass
235
236 @abc.abstractmethod
237 def _str_rstrip(self, to_strip=None):
238 pass
239
240 @abc.abstractmethod
241 def _str_removeprefix(self, prefix: str) -> Series:
242 pass
243
244 @abc.abstractmethod
245 def _str_removesuffix(self, suffix: str) -> Series:
246 pass
247
248 @abc.abstractmethod
249 def _str_split(
250 self, pat=None, n=-1, expand: bool = False, regex: bool | None = None
251 ):
252 pass
253
254 @abc.abstractmethod
255 def _str_rsplit(self, pat=None, n=-1):
256 pass
257
258 @abc.abstractmethod
259 def _str_extract(self, pat: str, flags: int = 0, expand: bool = True):
260 pass