as the title suggests, I have developed a function that, given an ORDERED ascending list, you keep only the elements which have a distance of at least k periods but it does so while dynamically changing the iterator while looping. I have been told this is to be avoided like the plague and, though I am not fully convinced as to why this is such a bad idea, I trust in those whom I have been leaning on for training and thus asking for advice on how to avoid such practice. The code is the following:
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 in order to avoid it? p.s.: do not worry about solutions in which the list is not ordered. the list that will be given will always be ordered in an ascending fashion.
The biggest default of your function his to have a quadratic complexity, since x = x[:i+1] + x[i+2:]
copy the whole x
each time.
The simplest an more efficient way to do that want is probably
a.resample('10D').first().index
.
If you prefer a loop you can just do :
def funz1(dates,k):
result=[dates[0]]
for date in dates:
if (date-result[-1]).days >= k:
result.append(date)
return result
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With