Geometric rounding with numpy/quantize?
<p>I've got a pandas series of data which is a curve.</p>

<p>I want to round it in such a way as to make it 'stepped'. Furthermore, I want the steps to be roughly within 10% of the present value. (Another way of putting this is I want the steps to increase in increments of 10%, i.e. geometrically).</p>

<p>I've written something that's iterative and slow:</p>

<pre><code>def chunk_trades(A):
    try:
        last = A[0]
    except:
        print(A)
        raise
    new = []
    for x in A.iteritems():
        if not last or np.abs((x[1]-last)/last) &gt; 0.1:
            new.append(x[1])
            last = x[1]
        else:
            new.append(last)
    s = pd.Series(new, index=A.index)
    return s
</code></pre>

<p>I don't want to use this code.</p>

<p>I'm trying to find a faster, pythonic way of doing this. I've tried using numpy.digitize() but I don't think that's what I'm looking for. Any ideas for how best to approach this?</p>

<p>OK, I think the solution should be something like:</p>

<pre><code>np.exp(np.around(np.log(np.abs(j)), decimals=1)) * np.sign(j)
</code></pre>

<p>Map to logarithmic space, do the rounding, transform back.</p>

python pandas numpy