Consider this code:
>>> arr = np.array(['A', 'B', 'C'])
>>> arr[0:20]
array(['A', 'B', 'C'],
dtype='|S1')
Why am I allowed to slice the array past 3
? I expected this to throw an an error, as there are not 20 entries to reference.
Consider this code:
>>> arr = np.array(['A', 'B', 'C'])
>>> arr[0:20]
array(['A', 'B', 'C'],
dtype='|S1')
Why am I allowed to slice the array past 3
? I expected this to throw an an error, as there are not 20 entries to reference.
That works for regular list slicing too, and string slicing, and tuple slicing, and most sliceable objects. It's just the Python convention to allow out-of-range slicing and adjust out-of-range slice endpoints to the nearest endpoint of the sequence.
This is taken from the documentation.
Given a slice expression like s[i:j:k]
,
The slice of s from i to j with step k is defined as the sequence of items with index x = i + n*k such that 0 <= n < (j-i)/k. In other words, the indices are i, i+k, i+2*k, i+3*k and so on, stopping when j is reached (but never including j). When k is positive, i and j are reduced to len(s) if they are greater
The same applies to numpy arrays. Given a slice expression, say x[0:20]
, it'll just be reduced to x[0:len(x)]
returning everything in the array.
Python gracefully handles out of bounds access to elements by returning as many elements as there are without throwing an error. This is a design decision taken by the developers of the language. You may or may not want the error, so do be cautious when slicing lists this way.