I have been trying to debug an error resulting from using the Alphalens module in zipline
The issue seems to be that for some reason the timezone attribute of the parameters to alphalens do not match even though the tests beforehand show that they do!
here is an example. The df at the top is just an output of a pipeline call.
Any help is appreciated. I have tried everything on my end!
data = df.copy(deep=True)
data.index.set_levels(pd.to_datetime(data.index.get_level_values(0), utc=True).values, level=0, inplace=True)
print(data[['MLfactor']].head())
print(data[['yesterday_close']].head())
print()
my_factor = data.tz_localize('UTC', level=0,).tz_convert(timezone('UTC'), level=0)[['MLfactor']]
closes = data.tz_localize('UTC', level=0,).tz_convert(timezone('UTC'), level=0)[['yesterday_close']]
print(my_factor.index.levels[0].tz, closes.index.levels[0].tz)
print(my_factor.index.levels[0].tz == closes.index.levels[0].tz)
factor_data = al.utils.get_clean_factor_and_forward_returns(factor=my_factor,
prices=closes,
# groupby=sectors,
#groupby_labels=MORNINGSTAR_SECTOR_CODES,
#periods=(1,5,10),
max_loss=10000,
)
output
MLfactor
2021-01-04 Equity(FIBBG000C2V3D6 [A]) 0.139287
Equity(FIBBG00B3T3HD3 [AA]) 0.150880
Equity(FIBBG005P7Q881 [AAL]) 0.127909
Equity(FIBBG00WCNDCZ6 [AAN]) 0.137289
Equity(FIBBG000C2LZP3 [AAON]) 0.132719
yesterday_close
2021-01-04 Equity(FIBBG000C2V3D6 [A]) 117.725
Equity(FIBBG00B3T3HD3 [AA]) 23.150
Equity(FIBBG005P7Q881 [AAL]) 16.052
Equity(FIBBG00WCNDCZ6 [AAN]) 17.820
Equity(FIBBG000C2LZP3 [AAON]) 66.675
UTC UTC
True
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-340-76baf73d515f> in <module>()
19 #groupby_labels=MORNINGSTAR_SECTOR_CODES,
20 #periods=(1,5,10),
---> 21 max_loss=10000,
22 )
23
/opt/conda/envs/zipline/lib/python3.6/site-packages/alphalens/utils.py in get_clean_factor_and_forward_returns(factor, prices, groupby, binning_by_group, quantiles, bins, periods, filter_zscore, groupby_labels, max_loss, zero_aware, cumulative_returns)
838 periods,
839 filter_zscore,
--> 840 cumulative_returns,
841 )
842
/opt/conda/envs/zipline/lib/python3.6/site-packages/alphalens/utils.py in compute_forward_returns(factor, prices, periods, filter_zscore, cumulative_returns)
261
262 factor_dateindex = factor.index.levels[0]
--> 263 if factor_dateindex.tz != prices.index.tz:
264 raise NonMatchingTimezoneError("The timezone of 'factor' is not the "
265 "same as the timezone of 'prices'. See "
AttributeError: 'MultiIndex' object has no attribute 'tz'