Skip to content

Commit bc65fe6

Browse files
RoeiRazWillAyd
authored andcommitted
Fix slicer assignment bug (#28131)
1 parent 612d3b2 commit bc65fe6

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

doc/source/whatsnew/v1.0.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ Interval
141141
Indexing
142142
^^^^^^^^
143143

144-
-
144+
- Bug in assignment using a reverse slicer (:issue:`26939`)
145145
-
146146

147147
Missing

pandas/core/indexers.py

+1
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ def length_of_indexer(indexer, target=None) -> int:
226226
if step is None:
227227
step = 1
228228
elif step < 0:
229+
start, stop = stop + 1, start + 1
229230
step = -step
230231
return (stop - start + step - 1) // step
231232
elif isinstance(indexer, (ABCSeries, ABCIndexClass, np.ndarray, list)):

pandas/tests/indexing/test_loc.py

+10
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,16 @@ def test_series_indexing_zerodim_np_array(self):
10701070
result = s.loc[np.array(0)]
10711071
assert result == 1
10721072

1073+
def test_loc_reverse_assignment(self):
1074+
# GH26939
1075+
data = [1, 2, 3, 4, 5, 6] + [None] * 4
1076+
expected = Series(data, index=range(2010, 2020))
1077+
1078+
result = pd.Series(index=range(2010, 2020))
1079+
result.loc[2015:2010:-1] = [6, 5, 4, 3, 2, 1]
1080+
1081+
tm.assert_series_equal(result, expected)
1082+
10731083

10741084
def test_series_loc_getitem_label_list_missing_values():
10751085
# gh-11428

0 commit comments

Comments
 (0)