base.py 5.3 KB

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