Skip to content

Commit dfb2f45

Browse files
jbrockmendelroberthdevries
authored andcommitted
BUG: catch almost-null-slice in _convert_slice_indexer (pandas-dev#31866)
1 parent 7a3d4ae commit dfb2f45

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

pandas/core/indexes/base.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -3146,8 +3146,7 @@ def _convert_slice_indexer(self, key: slice, kind: str_t):
31463146
def is_int(v):
31473147
return v is None or is_integer(v)
31483148

3149-
is_null_slicer = start is None and stop is None
3150-
is_index_slice = is_int(start) and is_int(stop)
3149+
is_index_slice = is_int(start) and is_int(stop) and is_int(step)
31513150
is_positional = is_index_slice and not (
31523151
self.is_integer() or self.is_categorical()
31533152
)
@@ -3177,7 +3176,7 @@ def is_int(v):
31773176
except KeyError:
31783177
pass
31793178

3180-
if is_null_slicer:
3179+
if com.is_null_slice(key):
31813180
indexer = key
31823181
elif is_positional:
31833182
indexer = key

pandas/tests/indexes/test_base.py

+15
Original file line numberDiff line numberDiff line change
@@ -2611,3 +2611,18 @@ def test_validate_1d_input():
26112611
ser = pd.Series(0, range(4))
26122612
with pytest.raises(ValueError, match=msg):
26132613
ser.index = np.array([[2, 3]] * 4)
2614+
2615+
2616+
def test_convert_almost_null_slice(indices):
2617+
# slice with None at both ends, but not step
2618+
idx = indices
2619+
2620+
key = slice(None, None, "foo")
2621+
2622+
if isinstance(idx, pd.IntervalIndex):
2623+
with pytest.raises(ValueError, match="cannot support not-default step"):
2624+
idx._convert_slice_indexer(key, "loc")
2625+
else:
2626+
msg = "'>=' not supported between instances of 'str' and 'int'"
2627+
with pytest.raises(TypeError, match=msg):
2628+
idx._convert_slice_indexer(key, "loc")

0 commit comments

Comments
 (0)