I'd like to filter out columns within each group of 'ticker' so that what remains are only rows exactly 1, 2, 3, etc. years before my most recent time stamp within each ticker. For example:
data
index date ticker dividends
0 3/31/2019 AA 0.626
1 12/31/2018 AA 0.611
2 9/30/2018 AA 0.611
3 6/30/2018 AA 0.04
4 3/31/2018 AA 0.035
5 12/31/2017 AA 0.02
6 12/31/2018 CC 0.035
7 6/30/2018 CC 0.075
8 12/31/2017 CC 0.04
9 12/31/2016 CC 0.035
10 12/31/2015 CC 0.04
11 6/30/2015 CC 0.06
I'd like:
index date ticker dividends
0 3/31/2019 AA 0.626
4 3/31/2018 AA 0.035
6 12/31/2018 CC 0.035
8 12/31/2017 CC 0.04
9 12/31/2016 CC 0.035
10 12/31/2015 CC 0.04
I've messed around for some time with data.groupby('ticker').filter ... but I just can't figure out a pythonic way to do this, especially given that within each group of 'ticker' the most recent date is not always the same and the spacing between dates within each group differs.