Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pandas/core/arrays/_arrow_string_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,16 @@ def _str_swapcase(self) -> Self:
return self._from_pyarrow_array(pc.utf8_swapcase(self._pa_array))

def _str_removeprefix(self, prefix: str):
if prefix == "":
return self._from_pyarrow_array(self._pa_array)
starts_with = pc.starts_with(self._pa_array, pattern=prefix)
removed = pc.utf8_slice_codeunits(self._pa_array, len(prefix))
result = pc.if_else(starts_with, removed, self._pa_array)
return self._from_pyarrow_array(result)

def _str_removesuffix(self, suffix: str):
if suffix == "":
return self._from_pyarrow_array(self._pa_array)
ends_with = pc.ends_with(self._pa_array, pattern=suffix)
removed = pc.utf8_slice_codeunits(self._pa_array, 0, stop=-len(suffix))
result = pc.if_else(ends_with, removed, self._pa_array)
Expand Down
14 changes: 12 additions & 2 deletions pandas/tests/strings/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,12 @@ def test_strip_lstrip_rstrip_args(any_string_dtype, method, exp):


@pytest.mark.parametrize(
"prefix, expected", [("a", ["b", " b c", "bc"]), ("ab", ["", "a b c", "bc"])]
"prefix, expected",
[
("a", ["b", " b c", "bc"]),
("ab", ["", "a b c", "bc"]),
("", ["ab", "a b c", "bc"]),
],
)
def test_removeprefix(any_string_dtype, prefix, expected):
ser = Series(["ab", "a b c", "bc"], dtype=any_string_dtype)
Expand All @@ -554,7 +559,12 @@ def test_removeprefix(any_string_dtype, prefix, expected):


@pytest.mark.parametrize(
"suffix, expected", [("c", ["ab", "a b ", "b"]), ("bc", ["ab", "a b c", ""])]
"suffix, expected",
[
("c", ["ab", "a b ", "b"]),
("bc", ["ab", "a b c", ""]),
("", ["ab", "a b c", "bc"]),
],
)
def test_removesuffix(any_string_dtype, suffix, expected):
ser = Series(["ab", "a b c", "bc"], dtype=any_string_dtype)
Expand Down
Loading