DataFrame.expanding does not work as expected

Course Name: Python for Trading: Basic, Section No: 8, Unit No: 12, Unit type: Notebook



I have a problem with the DataFrame.expanding(min_periods=): 



If I modify the value of min_periods in the example, than only the window changes, but not the values calculated for each row. Also the calculated values don't correlate with the values of the DataFrame.rolling() function, which is shown some lines before in the unit. 

Hi Wolfgang,



The expanding window aggregates all data points till the current time period. 'min_periods' is the minimum number of observations required to calculate the statistics. Let's see this with an example. Suppose you have 10 points in your data, and the minimum period is 5, and you want to calculate the sum. The first sum will be of the first 5 values and recorded against 5th observation. The next sum (i.e. the sum against the 6th observation) will be the sum of 6 values, the next will sum 7 values and so on. This means that the first position remains fixed, and the number of values while calculation increases unlike rolling where the number of observations is fixed and the first position shifts.









If you look at your output, for the minimum period 20, the first mean is recorded at the index 19. And for the minimum period 21, it is recorded at the index 21. In both cases, the mean value at a particular index is same 

because it is the mean of all the observations up to that time.



I hope this helps.



Thanks,

Satyapriya

 

Hi Satyapriya



thanks a lot for your introduction. I made an own example too, now it's clear for me. Only in the context of the course some doubts remain.  



Bye,

Wolfgang

Hi Wolfgang



Feel free to reach out to us in case you need clarity with any further doubts you have.