Skip to content

Commit 5183066

Browse files
jbrockmendelukarroum
authored andcommitted
BUG: slice_canonize incorrectly raising (pandas-dev#37524)
* BUG: slice_canonize incorrectly raising * GH ref
1 parent 6444c29 commit 5183066

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

pandas/_libs/internals.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ cdef slice slice_canonize(slice s):
207207
Convert slice to canonical bounded form.
208208
"""
209209
cdef:
210-
Py_ssize_t start = 0, stop = 0, step = 1, length
210+
Py_ssize_t start = 0, stop = 0, step = 1
211211

212212
if s.step is None:
213213
step = 1
@@ -239,7 +239,7 @@ cdef slice slice_canonize(slice s):
239239
if stop > start:
240240
stop = start
241241

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

245245
if stop < 0:

pandas/tests/internals/test_internals.py

+7
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,13 @@ def test_zero_step_raises(self, slc):
922922
with pytest.raises(ValueError, match=msg):
923923
BlockPlacement(slc)
924924

925+
def test_slice_canonize_negative_stop(self):
926+
# GH#37524 negative stop is OK with negative step and positive start
927+
slc = slice(3, -1, -2)
928+
929+
bp = BlockPlacement(slc)
930+
assert bp.indexer == slice(3, None, -2)
931+
925932
@pytest.mark.parametrize(
926933
"slc",
927934
[

0 commit comments

Comments
 (0)