Skip to content

Commit 0db3112

Browse files
Backport PR #31773: BUG: fix StringArray/PandasArray setitem with slice (#31923)
Co-authored-by: Joris Van den Bossche <[email protected]>
1 parent affd898 commit 0db3112

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

doc/source/whatsnew/v1.0.2.rst

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ Bug fixes
3434

3535
- Using ``pd.NA`` with :meth:`DataFrame.to_json` now correctly outputs a null value instead of an empty object (:issue:`31615`)
3636

37+
**Experimental dtypes**
38+
39+
- Fixed bug in setting values using a slice indexer with string dtype (:issue:`31772`)
40+
3741
.. ---------------------------------------------------------------------------
3842
3943
.. _whatsnew_102.contributors:

pandas/core/arrays/numpy_.py

-4
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,8 @@ def __setitem__(self, key, value):
244244
value = extract_array(value, extract_numpy=True)
245245

246246
key = check_array_indexer(self, key)
247-
scalar_key = lib.is_scalar(key)
248247
scalar_value = lib.is_scalar(value)
249248

250-
if not scalar_key and scalar_value:
251-
key = np.asarray(key)
252-
253249
if not scalar_value:
254250
value = np.asarray(value, dtype=self._ndarray.dtype)
255251

pandas/tests/extension/base/setitem.py

+23
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,29 @@ def test_setitem_tuple_index(self, data):
173173
s[(0, 1)] = data[1]
174174
self.assert_series_equal(s, expected)
175175

176+
def test_setitem_slice(self, data, box_in_series):
177+
arr = data[:5].copy()
178+
expected = data.take([0, 0, 0, 3, 4])
179+
if box_in_series:
180+
arr = pd.Series(arr)
181+
expected = pd.Series(expected)
182+
183+
arr[:3] = data[0]
184+
self.assert_equal(arr, expected)
185+
186+
def test_setitem_loc_iloc_slice(self, data):
187+
arr = data[:5].copy()
188+
s = pd.Series(arr, index=["a", "b", "c", "d", "e"])
189+
expected = pd.Series(data.take([0, 0, 0, 3, 4]), index=s.index)
190+
191+
result = s.copy()
192+
result.iloc[:3] = data[0]
193+
self.assert_equal(result, expected)
194+
195+
result = s.copy()
196+
result.loc[:"c"] = data[0]
197+
self.assert_equal(result, expected)
198+
176199
def test_setitem_slice_mismatch_length_raises(self, data):
177200
arr = data[:5]
178201
with pytest.raises(ValueError):

pandas/tests/extension/test_numpy.py

+8
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,14 @@ def test_setitem_scalar_key_sequence_raise(self, data):
396396
# Failed: DID NOT RAISE <class 'ValueError'>
397397
super().test_setitem_scalar_key_sequence_raise(data)
398398

399+
@skip_nested
400+
def test_setitem_slice(self, data, box_in_series):
401+
super().test_setitem_slice(data, box_in_series)
402+
403+
@skip_nested
404+
def test_setitem_loc_iloc_slice(self, data):
405+
super().test_setitem_loc_iloc_slice(data)
406+
399407

400408
@skip_nested
401409
class TestParsing(BaseNumPyTests, base.BaseParsingTests):

0 commit comments

Comments
 (0)