def map_offsets(x):
'''Calculate the day offsets'''
days = [x for x in (pd_rolling - x).days if x >= 0]
days += [np.nan] * (len(pd_rolling) - len(days))
return [days[i] for i in periods_offsets]
df = pd.DataFrame({'dates': pd.date_range('2017-01-01', periods=8, freq='1d')}).set_index('dates')
# the inclusive ends of the time periods
rolling_dates = ['2017-01-02', '2017-01-05', '2017-01-07', '2017-01-08']
# the remaining time periods columns
periods_offsets = [0, 1, 2]
# Added: Casting to datetime will make offset calculation easier
pd_rolling = pd.to_datetime(rolling_dates)
# Create new column names for periods
fmt = 'periods_expiry_days_{}'
columns = [fmt.format(x) for x in periods_offsets]
# Subtract index from rolling date values, and add to dataframe
df[columns] = pd.DataFrame(df.index.map(map_offsets).tolist(), index=df.index)