Я бы создал функцию, которая использует pd.date_range
с частотой месяцев:
Эта функция предполагает, что у вас есть три региона, но может быть изменена для большего.
def myFunction(df, periods, freq='M'):
# find the last date in the df
last = pd.to_datetime(df.Year*10000+df.Month*100+1,format='%Y%m%d').max()
# create new date range based on n periods with a freq of months
newDates = pd.date_range(start=last, periods=periods+1, freq=freq)
newDates = newDates[newDates>last]
newDates = newDates[:periods+1]
new_df = pd.DataFrame({'Date':newDates})[1:]
# convert Date to year and month columns
new_df['Year'] = new_df['Date'].dt.year
new_df['Month'] = new_df['Date'].dt.month
new_df.drop(columns='Date', inplace=True)
# add your three regions and ffill values
west = df[:-2].append([new_df], sort=False, ignore_index=True).ffill()
east = df[:-1].append([new_df], sort=False, ignore_index=True).ffill()
north = df.append([new_df], sort=False, ignore_index=True).ffill()
# append you three region dfs and drop duplicates
new = west.append([east,north], sort=False, ignore_index=True).drop_duplicates()
return new.sort_values(['Year', 'Month']).reset_index().drop(columns='index')
myFunction(df,3)
спериоды, равные трем, вернутся в следующие три месяца ...
Year Month Region Value1 Value2
0 2016 1 west 2.0 3.0
1 2016 1 east 4.0 5.0
2 2016 1 north 5.0 3.0
3 2016 2 west 6.0 4.0
4 2016 2 east 7.0 3.0
5 2016 12 west 2.0 3.0
6 2016 12 east 3.0 7.0
7 2016 12 north 6.0 8.0
8 2017 1 west 2.0 3.0
9 2018 7 west 1.0 1.0
10 2018 7 east 9.0 9.0
11 2018 7 north 5.0 1.0
12 2018 8 west 1.0 1.0
13 2018 8 east 9.0 9.0
14 2018 8 north 5.0 1.0
15 2018 9 west 1.0 1.0
16 2018 9 east 9.0 9.0
17 2018 9 north 5.0 1.0
18 2018 10 west 1.0 1.0
19 2018 10 east 9.0 9.0
20 2018 10 north 5.0 1.0