From 32bcb7da295b4c2b7fa91770ccf5c4cf09a35e86 Mon Sep 17 00:00:00 2001 From: wdyy20041223 <2795352227@qq,com> Date: Thu, 6 Nov 2025 21:59:20 +0800 Subject: [PATCH] TST: Add regression tests for setitem with different string storage (GH#52987) --- pandas/tests/strings/test_strings.py | 30 ++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/pandas/tests/strings/test_strings.py b/pandas/tests/strings/test_strings.py index 283c7ad50c814..e07ece91090df 100644 --- a/pandas/tests/strings/test_strings.py +++ b/pandas/tests/strings/test_strings.py @@ -818,3 +818,33 @@ def test_decode_with_dtype_none(): result = ser.str.decode("utf-8", dtype=None) expected = Series(["a", "b", "c"], dtype="str") tm.assert_series_equal(result, expected) + + +def test_setitem_with_different_string_storage(): + # GH#52987 + # Test setitem with values from different string storage type + pytest.importorskip("pyarrow") + + # Test Series[string[python]].__setitem__(Series[string[pyarrow]]) + ser_python = Series(range(5), dtype="string[python]") + ser_pyarrow = ser_python.astype("string[pyarrow]") + + ser_python[:2] = ser_pyarrow[:2] + expected = Series(["0", "1", "2", "3", "4"], dtype="string[python]") + tm.assert_series_equal(ser_python, expected) + + # Test Series[string[pyarrow]].__setitem__(Series[string[python]]) + ser_pyarrow = Series(range(5), dtype="string[pyarrow]") + ser_python = ser_pyarrow.astype("string[python]") + + ser_pyarrow[:2] = ser_python[:2] + expected = Series(["0", "1", "2", "3", "4"], dtype="string[pyarrow]") + tm.assert_series_equal(ser_pyarrow, expected) + + # Test with slice and missing values + ser_python = Series(["a", "b", None, "d", "e"], dtype="string[python]") + ser_pyarrow = Series(["X", "Y", None], dtype="string[pyarrow]") + + ser_python[1:4] = ser_pyarrow + expected = Series(["a", "X", "Y", NA, "e"], dtype="string[python]") + tm.assert_series_equal(ser_python, expected)