| 241 | ) |
| 242 | |
| 243 | def _re_compile( |
| 244 | self, |
| 245 | *, |
| 246 | pat: str | bytes | Pattern | Any, |
| 247 | flags: int = 0, |
| 248 | case: bool | None = None, |
| 249 | ) -> Pattern | Any: |
| 250 | is_compiled_re = isinstance(pat, re.Pattern) |
| 251 | |
| 252 | if is_compiled_re and flags != 0: |
| 253 | raise ValueError("Flags cannot be set when pat is a compiled regex.") |
| 254 | |
| 255 | if is_compiled_re and case is not None: |
| 256 | raise ValueError("Case cannot be set when pat is a compiled regex.") |
| 257 | |
| 258 | if is_compiled_re: |
| 259 | # no-op, needed to tell mypy this isn't a string |
| 260 | return re.compile(pat) |
| 261 | |
| 262 | if case is None: |
| 263 | case = True |
| 264 | |
| 265 | # The case is handled by the re flags internally. |
| 266 | # Add it to the flags if necessary. |
| 267 | if not case: |
| 268 | flags |= re.IGNORECASE |
| 269 | |
| 270 | if getattr(pat, "dtype", None) != np.object_: |
| 271 | pat = self._stringify(pat) |
| 272 | |
| 273 | def func(x): |
| 274 | return re.compile(x, flags=flags) |
| 275 | |
| 276 | if isinstance(pat, np.ndarray): |
| 277 | # apply_ufunc doesn't work for numpy arrays with output object dtypes |
| 278 | func_ = np.vectorize(func) |
| 279 | return func_(pat) |
| 280 | else: |
| 281 | return _apply_str_ufunc(func=func, obj=pat, dtype=np.object_) |
| 282 | |
| 283 | def len(self) -> T_DataArray: |
| 284 | """ |