You can convert index
to Series
, shifting and compare index by TTM
, filter and add one year, last convert back to YYYY-MM
string and pass to rename
:
s = df.index.to_series().shift()
s1 = pd.to_datetime(s[s.index == 'TTM'], format='%Y-%m') + pd.DateOffset(years=1)
print (s1)
TTM 2020-12-01
dtype: datetime64[ns]
df = df.rename(index=s1.dt.strftime('%Y-%m'))
print (df)
Book Value Per Share * IDR
2010-12 NaN
2011-12 326.22
2012-12 484.66
2013-12 596.52
2014-12 740.09
2015-12 878.66
2016-12 1139.92
2017-12 1292.85
2018-12 1417.75
2019-12 1612.50
2020-12 1567.89
Last if need DatetimeIndex:
df.index = pd.to_datetime(df.index, format='%Y-%m')
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…