[Numpy-discussion] better error message possible?

Thouis (Ray) Jones thouis@gmail....
Thu Jun 7 05:36:12 CDT 2012


On Thu, Jun 7, 2012 at 11:44 AM, Dave Hirschfeld
<dave.hirschfeld@gmail.com> wrote:
> Paul Anton Letnes <paul.anton.letnes <at> gmail.com> writes:
>
>> I would prefer:
>> IndexError: index 3 is out of bounds for axis 0: [-3,2]
>> as I find the 3) notation a bit weird - after all, indices are not floats, so
> 2.999 or 2.3 doesn't make sense as
>> an index.
>>
>> An alternative is to not refer to negative indices and say
>> IndexError: index 3 is out of bounds for axis 0, shape was: (3,)
>> (print more axes when the number of axes is higher.)
>>
>
> +1 for the latter suggestion - if the array shape is available it's a great
> help in debugging the error.

I agree that reporting the shape would be good, but it's usually not
available at the point that the indices are found to be out-of-bounds,
due to (often implicit) flattening.  I think it might be possible to
track and report that the array was flattened, which might help avoid
some confusion when the maximum index reported in the Exception
doesn't match any of the dimensions of the array being indexed due to
flattening.

Another possibility I entertained was to split the too-high vs. too-low cases:
IndexError: index 3 is out of bounds for axis 0: must be < 3
IndexError: index -4 is out of bounds for axis 0: must be >= -3

Ray Jones


More information about the NumPy-Discussion mailing list