Skip to content

Commit a9e7d2b

Browse files
jbrockmendeljorisvandenbossche
authored andcommitted
BUG (string): str.replace with negative n (#59628)
* BUG (string): str.replace with negative n * update GH ref
1 parent 616ede5 commit a9e7d2b

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
@@ -352,9 +352,7 @@ def _str_replace(
352352
fallback_performancewarning()
353353
return super()._str_replace(pat, repl, n, case, flags, regex)
354354

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

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

pandas/tests/extension/test_arrow.py

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

1843+
# Same bug for pyarrow-backed StringArray GH#59628
1844+
ser2 = ser.astype(pd.StringDtype(storage="pyarrow"))
1845+
actual2 = ser2.str.replace("a", "", -3, True)
1846+
expected2 = expected.astype(ser2.dtype)
1847+
tm.assert_series_equal(expected2, actual2)
1848+
1849+
ser3 = ser.astype(pd.StringDtype(storage="pyarrow", na_value=np.nan))
1850+
actual3 = ser3.str.replace("a", "", -3, True)
1851+
expected3 = expected.astype(ser3.dtype)
1852+
tm.assert_series_equal(expected3, actual3)
1853+
18431854

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

0 commit comments

Comments
 (0)