Skip to content

Commit d1f0bba

Browse files
committed
BUG: ser.loc[range] = foo treating key as positional
1 parent 6b43a78 commit d1f0bba

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

pandas/core/indexing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,8 @@ def _get_setitem_indexer(self, key):
665665
return self._convert_tuple(key)
666666

667667
if isinstance(key, range):
668-
return list(key)
668+
# test_loc_setitem_range_key
669+
key = list(key)
669670

670671
return self._convert_to_indexer(key, axis=0)
671672

pandas/tests/indexing/test_loc.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,6 +1555,19 @@ def test_loc_setitem_cast3(self):
15551555
df.one = np.int8(7)
15561556
assert df.dtypes.one == np.dtype(np.int8)
15571557

1558+
def test_loc_setitem_range_key(self, frame_or_series):
1559+
# don't treat range key as positional
1560+
obj = frame_or_series(range(5), index=[3, 4, 1, 0, 2])
1561+
1562+
values = [9, 10, 11]
1563+
if obj.ndim == 2:
1564+
values = [[9], [10], [11]]
1565+
1566+
obj.loc[range(3)] = values
1567+
1568+
expected = frame_or_series([0, 1, 10, 9, 11], index=obj.index)
1569+
tm.assert_equal(obj, expected)
1570+
15581571

15591572
class TestLocWithEllipsis:
15601573
@pytest.fixture(params=[tm.loc, tm.iloc])

0 commit comments

Comments
 (0)