as the name implies, I developed a function that, given the ORDERED list, only stores elements that have a distance of at least k periods, but this happens when the iterator dynamically changes during the loop. I was told that this should be avoided like the plague, and although I am not entirely convinced why this is such a bad idea, I believe in those whom I tend to learn, and therefore I ask for advice on how to avoid such practice. The code is as follows:
import pandas as pd from datetime import days a = pd.Series(range(0,25,1), index=pd.date_range('2011-1-1',periods=25)) store_before_cleanse = a.index def funz(x,k): i = 0 while i < len(x)-1: if (x[i+1]-x[i]).days < k: x = x[:i+1] + x[i+2:] i = i-1 i = i + 1 return x print(funz(store_before_cleanse,10))
What do you think can be done to avoid this? ps: don't worry about solutions in which the list is not ordered. the list to be specified will always be sorted in ascending order.
source share