[Numpy-discussion] non-integer index misfeature?
Wed Dec 12 14:48:42 CST 2012
On Wed, Dec 12, 2012 at 8:20 PM, Ralf Gommers <firstname.lastname@example.org> wrote:
> On Tue, Dec 11, 2012 at 5:44 PM, Neal Becker <email@example.com> wrote:
>> I think it's a misfeature that a floating point is silently accepted as an
>> index. I would prefer a warning for:
>> bins = np.arange (...)
>> for b in bins:
>> w[b] = blah
>> when I meant:
>> for ib,b in enumerate (bins):
>> w[ib] = blah
> Agreed. Scipy.special functions were just changed to generate warnings on
> truncation of float inputs where ints are expected (only if truncation
> changes the value, so 3.0 is silent and 3.1 is not).
> For numpy indexing this may not be appropriate though; checking every index
> value used could slow things down and/or be quite disruptive.
I doubt this is measurable, and it only affects people who are using
floats as indexes, which is a risky thing to be doing in the first
place. The only good reason to use floats as indexes is if you're
doing floating point arithmetic to calculate indexes -- but now you're
going to get bitten as soon as some operation returns N - epsilon
instead of N, and gets truncated to N - 1.
I'd be +1 for a patch to make numpy warn when indexing with
non-integer floats. (Heck, I'd probably be +1 on deprecating allowing
floating point numbers as indexes at all... it's risky as heck and
reminding people to think about rounding can only be a good thing,
given that risk.)
More information about the NumPy-Discussion