Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Closed
scipy-gitbot opened this Issue · 2 comments

2 participants

@scipy-gitbot

Original ticket http://projects.scipy.org/scipy/ticket/1372 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 "reverse.py", line 1154, in <module>
    long_stats.update_all_stats(sig,current)
  File "reverse.py", line 130, in update_all_stats
    self.stats[ii] = self.infra[ii][0](sig,current)
  File "reverse.py", line 82, in <lambda>
    fn = lambda sig,current: pct(sig['diff_600'][sig['std_diff_600']>0],1,85,current,7200)
  File "reverse.py", 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/stats.py", 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
    else:
        return _interpolate(values[int(idx)], values[int(idx) + 1], idx % 1)
@scipy-gitbot

@WarrenWeckesser wrote on 2011-01-27

Add markup to the description for the traceback and code.

@rgommers
Owner

Fixed by gh-3186.

@ev-br ev-br closed this in #3186
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.