Given the table below, with only date and number columns given, I want to infer avg_last_2m column (which calculate average value for number column for last 2 months) which is given as follows:
For example,
Given the date 20190909, last 2 month will be starting from date 20190709 to date 20190908, within that period, we have date 20190805 (with number =1), therefore average last 2 month would be 1/1=1.0.
Another example would be 20190930, last 2 month will be starting from date 20190730 to date 20190929, we have date 20190805 (with number = 1) and date 20190909 (with number=0), therefore average of last 2 month would be (1+0)/2=0.5.
How should we compute the column avg_last_2m, based on columns date and number? Efficiency is of some important here, as I would have around 100k rows of data in reality.
This is the code for the data frame
test_data=pd.DataFrame({'date':['20190606','20190610','20190708','20190805','20190909','20190930'],'number':[3,5,4,1,0,0],\
'avg_last_2m':[None,3,4,4,1,0.5]})
