0

Why does numpy returns an empty array when you subset an array with indices higher than the shape of the array? is this somewhat a bug or what is the advantage of this? I would expect an error message, like the one you get if you try to plot an index out of bounds. Example:

>>> import numpy as np
>>> from matplotlib import pyplot as plt
>>> a = np.array([[1, 2, 3], [4, 5, 6]])
>>> np.shape(a)
(2, 3)

This obviously makes no sense, since a is an array with 3 columns, but instead of returning an error message, you get an empty array, and furthermore, you can do operations with it:

>>> (a[:, 44:55])
array([], shape=(2, 0), dtype=int64)
>>> np.sum(a[:, 44:55], 1)
array([0, 0])

What is the advantage of this? I just cannot see the use of it. Is there a way to prevent this from happening?

Laura
  • 1,822
  • 3
  • 15
  • 23
  • 2
    Python slice behavior works this way for lists, slices that would take you out-of-bounds simply ignore the out of bounds stuff. The makers of `numpy` chose to implement `numpy.ndarray` slicing this way. It is not a bug. You can always add the bounds checking yourself if you need it. – juanpa.arrivillaga Feb 19 '18 at 23:19
  • 2
    That's expected behavior, copied from the way Python's built-in sequences handle out-of-range slicing. I don't know if the NumPy developers agreed with Python's design decisions, but back when this NumPy behavior was decided, it would have been difficult to make different decisions; several assumptions about slicing behavior were baked into non-configurable parts of the core slicing mechanics. For example, an omitted end index was replaced with `sys.maxsize`, assuming the sequence would treat it as equivalent to the sequence length. – user2357112 Feb 19 '18 at 23:28
  • A common use of this feature is `sys.argv[1:]` to check for commandline arguments. – hpaulj Feb 20 '18 at 04:48

0 Answers0