Skip to content

Commit 306385b

Browse files
authored
BUG (string): str.replace with negative n (#59628)
* BUG (string): str.replace with negative n * update GH ref
1 parent 172e477 commit 306385b

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

doc/source/whatsnew/v2.3.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ Conversion
102102

103103
Strings
104104
^^^^^^^
105-
-
105+
- Bug in :meth:`Series.str.replace` when ``n < 0`` for :class:`StringDtype` with ``storage="pyarrow"`` (:issue:`59628`)
106106
-
107107

108108
Interval

pandas/core/arrays/string_arrow.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,7 @@ def _str_replace(
350350
fallback_performancewarning()
351351
return super()._str_replace(pat, repl, n, case, flags, regex)
352352

353-
func = pc.replace_substring_regex if regex else pc.replace_substring
354-
result = func(self._pa_array, pattern=pat, replacement=repl, max_replacements=n)
355-
return type(self)(result)
353+
return ArrowExtensionArray._str_replace(self, pat, repl, n, case, flags, regex)
356354

357355
def _str_repeat(self, repeats: int | Sequence[int]):
358356
if not isinstance(repeats, int):

pandas/tests/extension/test_arrow.py

+11
Original file line numberDiff line numberDiff line change
@@ -1855,6 +1855,17 @@ def test_str_replace_negative_n():
18551855
expected = pd.Series(["bc", ""], dtype=ArrowDtype(pa.string()))
18561856
tm.assert_series_equal(expected, actual)
18571857

1858+
# Same bug for pyarrow-backed StringArray GH#59628
1859+
ser2 = ser.astype(pd.StringDtype(storage="pyarrow"))
1860+
actual2 = ser2.str.replace("a", "", -3, True)
1861+
expected2 = expected.astype(ser2.dtype)
1862+
tm.assert_series_equal(expected2, actual2)
1863+
1864+
ser3 = ser.astype(pd.StringDtype(storage="pyarrow", na_value=np.nan))
1865+
actual3 = ser3.str.replace("a", "", -3, True)
1866+
expected3 = expected.astype(ser3.dtype)
1867+
tm.assert_series_equal(expected3, actual3)
1868+
18581869

18591870
def test_str_repeat_unsupported():
18601871
ser = pd.Series(["abc", None], dtype=ArrowDtype(pa.string()))

0 commit comments

Comments
 (0)