python - Best way to handle ZeroDivisonError? -
i've got following code:
def chunk_trades(a): last = a[0] new = [] x in a.iteritems(): if np.abs((x[1]-last)/last) > 0.1: new.append(x[1]) last = x[1] else: new.append(last) s = pd.series(new, index=a.index) return s
sometimes last
can zero. in case, i'd carry on gracefully if last
zero.
what's cleanest way?
just replace line this:
if not last or np.abs((x[1]-last)/last) > 0.1:
this not raise exception since left assertion checked first.
Comments
Post a Comment