scoreatpercentile() does not handle empty list inputs (Trac #1372) #1897

scipy-gitbot opened this Issue Apr 25, 2013 · 2 comments

2 participants


Original ticket on 2011-01-27 by trac user mpenning, assigned to unknown.

When iterating through slices of list structures, occasionally scoreatpercentile() may get called with an empty list, resulting in a IndexError traceback that looks similar to this:

Traceback (most recent call last):
  File "", line 1154, in <module>
  File "", line 130, in update_all_stats
    self.stats[ii] = self.infra[ii][0](sig,current)
  File "", line 82, in <lambda>
    fn = lambda sig,current: pct(sig['diff_600'][sig['std_diff_600']>0],1,85,current,7200)
  File "", line 887, in pct
    return pctl(array.truncate(before=current-dt.timedelta(seconds=backdelta),after=current).valid().values,value)
  File "/usr/lib/python2.5/site-packages/scipy/stats/", line 1287, in scoreatpercentile
    return _interpolate(values[int(idx)], values[int(idx) + 1], idx % 1)
IndexError: index out of bounds
[mpenning@Bucksnort data]$

scoreatpercentile() should return numpy.nan if the input list is empty...

The following code may be useful as a local substitute while the bug gets fixed...

def scoreatpercentile(a, per):
    values = np.sort(a,axis=0)

    _interpolate = lambda a,b,fraction: a + (b - a)*fraction

    idx = per /100. * (values.shape[0] - 1)
    if (idx % 1 == 0):
        return values[idx]
    elif values.shape[0]==0:
        return np.nan
        return _interpolate(values[int(idx)], values[int(idx) + 1], idx % 1)

@WarrenWeckesser wrote on 2011-01-27

Add markup to the description for the traceback and code.

SciPy member

Fixed by gh-3186.

@ev-br ev-br closed this in #3186 Jan 31, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment