Skip to content

Commit c98dbc7

Browse files
committed
Merge pull request #9996 from slonik-az/bugfix/issue-9995
BUGFIX: length_of_indexer() can return incorrect values that break slice assignments
2 parents e6c4f76 + 4cc0efb commit c98dbc7

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

doc/source/whatsnew/v0.16.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ Bug Fixes
205205
- Bug in ``to_msgpack`` and ``read_msgpack`` zlib and blosc compression support (:issue:`9783`)
206206

207207
- Bug ``GroupBy.size`` doesn't attach index name properly if grouped by ``TimeGrouper`` (:issue:`9925`)
208+
- Bug causing an exception in slice assignments because ``length_of_indexer`` returns wrong results (:issue:`9995`)
208209

209210

210211

pandas/core/indexing.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1591,8 +1591,8 @@ def length_of_indexer(indexer, target=None):
15911591
if step is None:
15921592
step = 1
15931593
elif step < 0:
1594-
step = abs(step)
1595-
return (stop - start) / step
1594+
step = -step
1595+
return (stop - start + step-1) // step
15961596
elif isinstance(indexer, (ABCSeries, Index, np.ndarray, list)):
15971597
return len(indexer)
15981598
elif not is_list_like_indexer(indexer):

pandas/tests/test_indexing.py

+7
Original file line numberDiff line numberDiff line change
@@ -1438,6 +1438,13 @@ def test_iloc_setitem_series(self):
14381438
result = s.iloc[:4]
14391439
assert_series_equal(result, expected)
14401440

1441+
s= Series([-1]*6)
1442+
s.iloc[0::2]= [0,2,4]
1443+
s.iloc[1::2]= [1,3,5]
1444+
result = s
1445+
expected= Series([0,1,2,3,4,5])
1446+
assert_series_equal(result, expected)
1447+
14411448
def test_iloc_setitem_list_of_lists(self):
14421449

14431450
# GH 7551

0 commit comments

Comments
 (0)