diff --git a/pandas/core/arrays/_arrow_string_mixins.py b/pandas/core/arrays/_arrow_string_mixins.py index e5e8ffe409788..4e32db15b392f 100644 --- a/pandas/core/arrays/_arrow_string_mixins.py +++ b/pandas/core/arrays/_arrow_string_mixins.py @@ -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) diff --git a/pandas/tests/strings/test_strings.py b/pandas/tests/strings/test_strings.py index e07ece91090df..5873800794d49 100644 --- a/pandas/tests/strings/test_strings.py +++ b/pandas/tests/strings/test_strings.py @@ -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) @@ -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)