Skip to content

BUG: catch almost-null-slice in _convert_slice_indexer #31866

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 23, 2020
5 changes: 2 additions & 3 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")