Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pandas/_libs/internals.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ cdef slice slice_canonize(slice s):
Convert slice to canonical bounded form.
"""
cdef:
Py_ssize_t start = 0, stop = 0, step = 1, length
Py_ssize_t start = 0, stop = 0, step = 1

if s.step is None:
step = 1
Expand Down Expand Up @@ -239,7 +239,7 @@ cdef slice slice_canonize(slice s):
if stop > start:
stop = start

if start < 0 or (stop < 0 and s.stop is not None):
if start < 0 or (stop < 0 and s.stop is not None and step > 0):
raise ValueError("unbounded slice")

if stop < 0:
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/internals/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,13 @@ def test_zero_step_raises(self, slc):
with pytest.raises(ValueError, match=msg):
BlockPlacement(slc)

def test_slice_canonize_negative_stop(self):
# GH#37524 negative stop is OK with negative step and positive start
slc = slice(3, -1, -2)

bp = BlockPlacement(slc)
assert bp.indexer == slice(3, None, -2)

@pytest.mark.parametrize(
"slc",
[
Expand Down