Python/Pandas For Loop Time Series

I am working with panel time-series data and am struggling with creating a fast for loop, to sum up, the past 50 numbers at the current i. The data is like 600k rows, and it starts to churn around 30k. Is there a way to use pandas or Numpy to do the same at a fraction of the time?

The change column is of type float, with 4 decimals.

Index     Change
0         0.0410
1         0.0000
2         0.1201
...     ... 
74327     0.0000
74328     0.0231
74329     0.0109
74330     0.0462
SEQ_LEN = 50
for i in range(SEQ_LEN, len(df)):
    df.at[i, 'Change_Sum'] = sum(df['Change'][i-SEQ_LEN:i])

Any help would be highly appreciated! Thank you!



Read more here: https://stackoverflow.com/questions/64403208/python-pandas-for-loop-time-series

Content Attribution

This content was originally published by AwhatLoop at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: