I've got the following code:
def chunk_trades(A): last = A[0] new = [] for 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 be zero. In this case, I'd like it to just carry on gracefully as if last was almost zero.
What's the cleanest way?
lastis zero, you want theifblock to execute, not theelseblock, right?if last == 0 or np.abs(...) > 0.1? Alternatively do exactly what you described, define anepsilon = 0.0000001and then do/ (last or epsilon). whenlast == 0it is considered false andepsilonwill be used in its place.