0

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?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Have you tried to create a list of lagged/shifted Series using a loop? – wwii Aug 17 '22 at 14:23
  • Does [How do I create variable variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) answer your question? – wwii Aug 17 '22 at 14:26
  • @Chris Make Multiple Shifted (Lagged) Columns in Pandas is for DataFrame. I do not want to convert my series into a dataframe as I need it to be a time series to perform regressions. If you have any suggestions please comment below. Thanks. – Galos Prigipas Aug 17 '22 at 14:44
  • @wwii I am not sure how to create this with a loop as the index is time and I do not know hoe to shift the time series using index – Galos Prigipas Aug 17 '22 at 14:45
  • @wwii I am not sure that the How do I create variable variables is for but it is not for time series. I am a Python beginner so I cannot identify if it is something that can solve my problem – Galos Prigipas Aug 17 '22 at 14:46

0 Answers0