I'm trying to count consecutive up days in equity return data - so if a positive day is 1 and a negative is 0, a list y=[0,0,1,1,1,0,0,1,0,1,1] should return z=[0,0,1,2,3,0,0,1,0,1,2].
I've come to a solution which is neat in terms of number of lines of code, but is very slow:
import pandas
y=pandas.Series([0,0,1,1,1,0,0,1,0,1,1])
def f(x):
return reduce(lambda a,b:reduce((a+b)*b,x)
z=pandas.expanding_apply(y,f)
I'm guessing I'm looping through the whole list y too many times. Is there a nice Pythonic way of achieving what I want while only going through the data once? I could write a loop myself but wondering if there's a better way.
Thanks!
解决方案
why the obsession with the ultra-pythonic way of doing things? readability + efficiency trumps "leet hackerz style."
I'd just do it like so:
a = [0,0,1,1,1,0,0,1,0,1,1]
b = [0,0,0,0,0,0,0,0,0,0,0]
for i in range(len(a)):
if a[i] == 1:
b[i] = b[i-1] + 1
else:
b[i] = 0