I have 2 dataframes I wish to merge:
df1 looks like this:
Date Col1 Col 2 Col 3 Col 4
2016-03 27.57 0.93 28.7 1.57
2016-04 25.83 0.23 28.34 0.84
2016-05 24.55 0.27 27.11 0.03
df2 looks like this:
Date ColA
2016-03-21 7.640769230769231
2016-03-22 7.739720279720279
2016-03-23 7.577311827956988
2016-03-24 7.745416666666666
As you can see, df1 is a monthly data and df2 is a daily data. However, I want to merge them in a daily format (following df2) but I also want df1 to be lagged (lag = -30)
This is my desired output:
Output:
Date ColA Col1 Col 2 Col 3 Col 4
2016-03-21 7.640769230769231 25.83 0.23 28.34 0.84
2016-03-22 7.739720279720279 25.83 0.23 28.34 0.84
2016-03-23 7.577311827956988 25.83 0.23 28.34 0.84
2016-03-24 7.745416666666666 25.83 0.23 28.34 0.84
....2016-04-01 xxxxxxxx 24.55 0.27 27.11 0.03
I tried this but, they just merge and the lags were not applied.
out = (df2.merge(df1.shift(-30), on='Date').axis=1)
EDIT: Since I can't make the below suggestions work on my specific problem, what I did is this:
out['Col1']= out['Col1'].shift(7).dropna()
out = pd.merge_asof(df1, df2, on = 'Date')
This is to lag only 1 column (which I decided to do since most columns have possibility to have different lag times.