您可以嘗試通過減去DataOffset
列出dfs
追加新Series
創造新DataFrame
,然後concat
他們。最後你可以日期時間。減去和d
是Timedelta
通過np.timedelta
到integer
轉換:
d = "2016.03.04"
#append substracted column maturity with DateOffset
dfs =[]
for i in range(5):
years_before = df['maturity'] - pd.DateOffset(years=i)
#get only datetime to date d
#print years_before.loc[years_before > d]
dfs.append(years_before.loc[years_before > d])
df = pd.DataFrame(pd.concat(dfs, ignore_index=True))
print df
maturity
0 2018-06-01
1 2017-10-01
2 2017-06-01
3 2016-10-01
4 2016-06-01
df['remain'] = (df['maturity'] - pd.to_datetime(d))/np.timedelta64(1, 'D')
#sort values by column maturity
df = df.sort_values('maturity')
print df
maturity remain
4 2016-06-01 89
3 2016-10-01 211
2 2017-06-01 454
1 2017-10-01 576
0 2018-06-01 819
我估計一下循環的最大數(不深入的檢驗):
#get max count of years => loops
maxYears = (df['maturity'].max() - pd.to_datetime(d))/np.timedelta64(1, 'D')/(365.25)
print maxYears
2.24229979466
#convert float to int, if 2.999 => 2, so one year is added
#rather add one more year (leap years, year is only estimated)
maxYears = int(maxYears) + 2
print maxYears
4