December 2018
Beginner to intermediate
684 pages
21h 9m
English
The goal is to predict returns over a given holding period. Hence, we need to align the features with return values with the corresponding return data point 1, 5, 10, or 20 days into the future for each equity. We achieve this by combining the pandas .groupby() method with the .shift() method as follows:
y = data.loc[:, return_cols]shifted_y = []for col in y.columns: t = int(re.search(r'\d+', col).group(0)) shifted_y.append(y.groupby(level='asset')['Returns{}D'.format(t)].shift(-t).to_frame(col))y = pd.concat(shifted_y, axis=1)y.info()MultiIndex: 47377 entries, (2014-01-02, Equity(24 [AAPL])) to (2015-12-31, Equity(47208 [GPRO]))Data columns (total 4 columns):Returns1D 47242 non-null float64Returns5D 46706 non-null ...