From b3646429d6553ea1454e9bbe37acf7a2f62d670f Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 30 Oct 2020 13:20:12 -0700 Subject: [PATCH 1/2] BUG: slice_canonize incorrectly raising --- pandas/_libs/internals.pyx | 4 ++-- pandas/tests/internals/test_internals.py | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/pandas/_libs/internals.pyx b/pandas/_libs/internals.pyx index 4f27fde52414a..006fd34632d5a 100644 --- a/pandas/_libs/internals.pyx +++ b/pandas/_libs/internals.pyx @@ -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 @@ -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: diff --git a/pandas/tests/internals/test_internals.py b/pandas/tests/internals/test_internals.py index 9d1a2bed5db12..10dd545e0355c 100644 --- a/pandas/tests/internals/test_internals.py +++ b/pandas/tests/internals/test_internals.py @@ -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#???? 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", [ From 0ef3718e0ef691851cf2b81af59787fe3faeb096 Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 30 Oct 2020 13:21:52 -0700 Subject: [PATCH 2/2] GH ref --- pandas/tests/internals/test_internals.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/internals/test_internals.py b/pandas/tests/internals/test_internals.py index 10dd545e0355c..84e71e5dafca3 100644 --- a/pandas/tests/internals/test_internals.py +++ b/pandas/tests/internals/test_internals.py @@ -923,7 +923,7 @@ def test_zero_step_raises(self, slc): BlockPlacement(slc) def test_slice_canonize_negative_stop(self): - # GH#???? negative stop is OK with negative step and positive start + # GH#37524 negative stop is OK with negative step and positive start slc = slice(3, -1, -2) bp = BlockPlacement(slc)