The // operator only returns int when the numerator is int, and it still returns float when the numerator is float.
assert type(2//2) == int assert type(2.//2) == float
To overcome this limitation in slices, you can use the following function:
def intslice(*args, **kwargs): '''Return a slice object that has integer boundaries. Example: np.arange(10)[intslice(10/10,10/2)] ''' args = [int(arg) for arg in args] kwargs = {key: int(arg) for key, arg in kwargs.items()} return slice(*args, **kwargs)
Now intslice(10/10,10/2) returns slice(1,5)
Vasco source share