diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 14ee21ea5614c..34bdd12988282 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -3163,8 +3163,7 @@ def _convert_slice_indexer(self, key: slice, kind: str_t): def is_int(v): return v is None or is_integer(v) - is_null_slicer = start is None and stop is None - is_index_slice = is_int(start) and is_int(stop) + is_index_slice = is_int(start) and is_int(stop) and is_int(step) is_positional = is_index_slice and not ( self.is_integer() or self.is_categorical() ) @@ -3194,7 +3193,7 @@ def is_int(v): except KeyError: pass - if is_null_slicer: + if com.is_null_slice(key): indexer = key elif is_positional: indexer = key diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 6327f1b03589b..22f6af2af4aed 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -2611,3 +2611,18 @@ def test_validate_1d_input(): ser = pd.Series(0, range(4)) with pytest.raises(ValueError, match=msg): ser.index = np.array([[2, 3]] * 4) + + +def test_convert_almost_null_slice(indices): + # slice with None at both ends, but not step + idx = indices + + key = slice(None, None, "foo") + + if isinstance(idx, pd.IntervalIndex): + with pytest.raises(ValueError, match="cannot support not-default step"): + idx._convert_slice_indexer(key, "loc") + else: + msg = "'>=' not supported between instances of 'str' and 'int'" + with pytest.raises(TypeError, match=msg): + idx._convert_slice_indexer(key, "loc")