df1
trade_date pct_chg
266 2019-09-20 0.2390
265 2019-09-23 -0.9769
264 2019-09-24 0.2776
263 2019-09-25 -1.0018
262 2019-09-26 -0.8914
261 2019-09-27 0.1182
260 2019-09-30 -0.9201
259 2019-10-08 0.2885
258 2019-10-09 0.3874
257 2019-10-10 0.7814
df2
obs_date last_recent_day_pct_chg last_second_day_pct_chg avg_l3d_pct_chg max_l3d_pct_chg
a 2019/9/21
b 2019/9/22
c 2019/9/23
d 2019/9/24
e 2019/9/25
f 2019/9/26
g 2019/9/27
h 2019/9/28
i 2019/9/29
j 2019/9/30
k 2019/10/5
l 2019/10/8
m 2019/10/9
n 2019/10/9
o 2019/9/29
Notice: There is only index and obs_date in df2
orginally.
Goal
I want to get value of df2
columns based on df1
as below:
- last_recent_day_pct_chg : If
obs_date
can be found intrade_date
, it will return corresponding pct_chg.(e.g. 2019/9/23: -0.9769). If not, it will return corresponding last_recent_day pct_chg.(e.g. 2019/9/21: 0.2390 , 2019/10/5:-0.9201). - last_second_day_pct_chg : If
obs_date
can be found intrade_date
,it will return corresponding last day pct_chg.(e.g. 2019/9/24: 0.2776).If not, it will return nearest corresponding last_second_day_pct_chg .(e.g. 2019/10/5: 0.1182 , 2019/9/23 : NULL). - avg_l3d_pct_chg/max_l3d_pct_chg: Based on
obs_date
, it returns average or max value of pct_chg from last recent 3trade_date
.(e.g. 2019/10/5, 2019/9/26,9/27,9/30, 2019/10/09, 2019/10/09,10/8, 9/30 )
Try
I consider shift method and this post.But it seems not working.