|
11 | 11 |
|
12 | 12 | from pandas.compat import (
|
13 | 13 | pa_version_under10p1,
|
| 14 | + pa_version_under11p0, |
14 | 15 | pa_version_under13p0,
|
15 | 16 | pa_version_under17p0,
|
16 | 17 | )
|
|
22 | 23 | import pyarrow.compute as pc
|
23 | 24 |
|
24 | 25 | if TYPE_CHECKING:
|
25 |
| - from collections.abc import ( |
26 |
| - Callable, |
27 |
| - Sized, |
28 |
| - ) |
| 26 | + from collections.abc import Callable |
29 | 27 |
|
30 | 28 | from pandas._typing import Scalar
|
31 | 29 |
|
32 | 30 |
|
33 | 31 | class ArrowStringArrayMixin:
|
34 |
| - _pa_array: Sized |
| 32 | + _pa_array: pa.ChunkedArray |
35 | 33 |
|
36 | 34 | def __init__(self, *args, **kwargs) -> None:
|
37 | 35 | raise NotImplementedError
|
@@ -93,12 +91,29 @@ def _str_get(self, i: int):
|
93 | 91 | selected = pc.utf8_slice_codeunits(
|
94 | 92 | self._pa_array, start=start, stop=stop, step=step
|
95 | 93 | )
|
96 |
| - null_value = pa.scalar( |
97 |
| - None, type=self._pa_array.type # type: ignore[attr-defined] |
98 |
| - ) |
| 94 | + null_value = pa.scalar(None, type=self._pa_array.type) |
99 | 95 | result = pc.if_else(not_out_of_bounds, selected, null_value)
|
100 | 96 | return type(self)(result)
|
101 | 97 |
|
| 98 | + def _str_slice( |
| 99 | + self, start: int | None = None, stop: int | None = None, step: int | None = None |
| 100 | + ): |
| 101 | + if pa_version_under11p0: |
| 102 | + # GH#59724 |
| 103 | + result = self._apply_elementwise(lambda val: val[start:stop:step]) |
| 104 | + return type(self)(pa.chunked_array(result, type=self._pa_array.type)) |
| 105 | + if start is None: |
| 106 | + if step is not None and step < 0: |
| 107 | + # GH#59710 |
| 108 | + start = -1 |
| 109 | + else: |
| 110 | + start = 0 |
| 111 | + if step is None: |
| 112 | + step = 1 |
| 113 | + return type(self)( |
| 114 | + pc.utf8_slice_codeunits(self._pa_array, start=start, stop=stop, step=step) |
| 115 | + ) |
| 116 | + |
102 | 117 | def _str_slice_replace(
|
103 | 118 | self, start: int | None = None, stop: int | None = None, repl: str | None = None
|
104 | 119 | ):
|
|
0 commit comments