I have imported a time series called m2:
print(m2)
1960-01-01 4.047453
1960-02-01 3.719152
1960-03-01 3.492393
1960-04-01 3.447087
1960-05-01 2.977413
...
2022-02-01 10.671207
2022-03-01 9.501048
2022-04-01 7.682554
2022-05-01 6.199250
2022-06-01 5.900724
Length: 750, dtype: float64
I want to create multiple new time series that will be lags of m2. For example, one time series starting 6 months ahead:
m2_lag_6 = m2.shift(6)
m2_lag_6.dropna()
1960-07-01 4.047453
1960-08-01 3.719152
1960-09-01 3.492393
1960-10-01 3.447087
1960-11-01 2.977413
...
2022-02-01 13.596152
2022-03-01 13.012511
2022-04-01 12.854070
2022-05-01 12.543622
2022-06-01 12.367188
I can do this for multiple lags by repeating the same process:
m2_lag_6 = m2.shift(6)
m2_lag_12 = m2.shift(12)
m2_lag_18 = m2.shift(18)
m2_lag_24 = m2.shift(24)
m2_lag_30 = m2.shift(30)
m2_lag_36 = m2.shift(36)
m2_lag_42 = m2.shift(42)
m2_lag_48 = m2.shift(48)
I would rather not have to repeat the same process multiple times. Any ideas on how I can do this?
Any help would be greatly appreciated.
Update: From Make Multiple Shifted (Lagged) Columns in Pandas
# Convert series to DataFrame
m2_df = pd.DataFrame({'Date': m2.index, 'M2': m2.values})
m2_df['Date'] = pd.to_datetime(m2_df['Date'])
m2_df = m2_df.set_index('Date')
lags = (6, 24, 6)
df = m2_df.assign(**{
f'{col} (t-{lag})': m2_df[col].shift(lag)
for lag in lags
for col in m2_df
})
Output:
M2 M2 (t-6) M2 (t-12) M2 (t-18)
Date
1960-01-01 4.047453 NaN NaN NaN
1960-02-01 3.719152 NaN NaN NaN
1960-03-01 3.492393 NaN NaN NaN
1960-04-01 3.447087 NaN NaN NaN
1960-05-01 2.977413 NaN NaN NaN
... ... ... ...
2022-02-01 10.671207 13.596152 26.889066 23.040523
2022-03-01 9.501048 13.012511 24.173474 23.795052
2022-04-01 7.682554 12.854070 18.279665 23.715350
2022-05-01 6.199250 12.543622 14.484839 24.392947
2022-06-01 5.900724 12.367188 12.857088 24.842843
[750 rows x 4 columns]
How I can split the DataFrame columns and convert them to timeseries, each being named after their header?
Additionally, is there a better way to do this without having to convert the time series into a dataframe and from a dataframe into series?