|
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 (
|
31 | 29 | Scalar,
|
|
34 | 32 |
|
35 | 33 |
|
36 | 34 | class ArrowStringArrayMixin:
|
37 |
| - _pa_array: Sized |
| 35 | + _pa_array: pa.ChunkedArray |
38 | 36 |
|
39 | 37 | def __init__(self, *args, **kwargs) -> None:
|
40 | 38 | raise NotImplementedError
|
@@ -96,13 +94,29 @@ def _str_get(self, i: int) -> Self:
|
96 | 94 | selected = pc.utf8_slice_codeunits(
|
97 | 95 | self._pa_array, start=start, stop=stop, step=step
|
98 | 96 | )
|
99 |
| - null_value = pa.scalar( |
100 |
| - None, |
101 |
| - type=self._pa_array.type, # type: ignore[attr-defined] |
102 |
| - ) |
| 97 | + null_value = pa.scalar(None, type=self._pa_array.type) |
103 | 98 | result = pc.if_else(not_out_of_bounds, selected, null_value)
|
104 | 99 | return type(self)(result)
|
105 | 100 |
|
| 101 | + def _str_slice( |
| 102 | + self, start: int | None = None, stop: int | None = None, step: int | None = None |
| 103 | + ) -> Self: |
| 104 | + if pa_version_under11p0: |
| 105 | + # GH#59724 |
| 106 | + result = self._apply_elementwise(lambda val: val[start:stop:step]) |
| 107 | + return type(self)(pa.chunked_array(result, type=self._pa_array.type)) |
| 108 | + if start is None: |
| 109 | + if step is not None and step < 0: |
| 110 | + # GH#59710 |
| 111 | + start = -1 |
| 112 | + else: |
| 113 | + start = 0 |
| 114 | + if step is None: |
| 115 | + step = 1 |
| 116 | + return type(self)( |
| 117 | + pc.utf8_slice_codeunits(self._pa_array, start=start, stop=stop, step=step) |
| 118 | + ) |
| 119 | + |
106 | 120 | def _str_slice_replace(
|
107 | 121 | self, start: int | None = None, stop: int | None = None, repl: str | None = None
|
108 | 122 | ) -> Self:
|
|
0 commit comments