Skip to content

Commit 24b2f38

Browse files
committed
REG: DataFrame.__setitem__(slice) is positional, closes pandas-dev#31469
1 parent ba08390 commit 24b2f38

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

pandas/core/frame.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -2937,8 +2937,11 @@ def __setitem__(self, key, value):
29372937
self._set_item(key, value)
29382938

29392939
def _setitem_slice(self, key, value):
2940+
# NB: we can't just use self.loc[key] = value because that
2941+
# operates on labels and we need to operate positional for
2942+
# backwards-compat, xref GH#31469
29402943
self._check_setitem_copy()
2941-
self.loc[key] = value
2944+
self.loc._setitem_with_indexer(key, value)
29422945

29432946
def _setitem_array(self, key, value):
29442947
# also raises Exception if object array with NA values

pandas/tests/frame/indexing/test_indexing.py

+9
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,15 @@ def test_fancy_getitem_slice_mixed(self, float_frame, float_string_frame):
860860

861861
assert (float_frame["C"] == 4).all()
862862

863+
def test_setitem_slice_position(self):
864+
# GH#31469
865+
df = pd.DataFrame(np.zeros((100, 1)))
866+
df[-4:] = 1
867+
arr = np.zeros((100, 1))
868+
arr[-4:] = 1
869+
expected = pd.DataFrame(arr)
870+
tm.assert_frame_equal(df, expected)
871+
863872
def test_getitem_setitem_non_ix_labels(self):
864873
df = tm.makeTimeDataFrame()
865874

0 commit comments

Comments
 (0)