pyfolio icon indicating copy to clipboard operation
pyfolio copied to clipboard

Allow passing a sector_mapping that changes with time

Open gusgordon opened this issue 8 years ago • 3 comments

Currently, pyfolio only allows sector mappings that are constant with time. Very occasionally, however, stocks can change sectors, which makes the mapping incorrect for some lookback periods. This can cause confusion.

One simple way to do this would be to also allow passing a DataFrame of sector mappings with time, where the frequency of the index can be daily, monthly, etc.

gusgordon avatar May 19 '17 18:05 gusgordon

Hi, The issue may not directly related to what you said before. It is related to the sector_mapping_example. When I execute this cell : "pf.create_position_tear_sheet(returns, positions, sector_mappings=sect_map)", I get the following error:

"ValueError Traceback (most recent call last) in () ----> 1 pf.create_position_tear_sheet(returns, positions, sector_mappings=sect_map)

C:\Python27\envs\stocks_lab\lib\site-packages\pyfolio\plotting.pyc in call_w_context(*args, **kwargs) 47 if set_context: 48 with context(): ---> 49 return func(*args, **kwargs) 50 else: 51 return func(*args, **kwargs)

C:\Python27\envs\stocks_lab\lib\site-packages\pyfolio\tears.pyc in create_position_tear_sheet(returns, positions, gross_lev, show_and_plot_top_pos, hide_positions, return_fig, sector_mappings) 437 positions_alloc = pos.get_percent_alloc(positions) 438 --> 439 plotting.plot_exposures(returns, positions_alloc, ax=ax_exposures) 440 441 plotting.show_and_plot_top_positions(

C:\Python27\envs\stocks_lab\lib\site-packages\pyfolio\plotting.pyc in plot_exposures(returns, positions_alloc, ax, **kwargs) 915 kind='line', style=['-g', '-r', '--k'], alpha=1.0, 916 ax=ax, **kwargs) --> 917 df_cum_rets = timeseries.cum_returns(returns, starting_value=1) 918 ax.set_xlim((df_cum_rets.index[0], df_cum_rets.index[-1])) 919 ax.set_title("Long/Short Exposure")

C:\Python27\envs\stocks_lab\lib\site-packages\pyfolio\timeseries.pyc in cum_returns(returns, starting_value) 606 # Note that we can't add that ourselves as we don't know which dt 607 # to use. --> 608 if pd.isnull(returns.iloc[0]): 609 returns.iloc[0] = 0 610

C:\Python27\envs\stocks_lab\lib\site-packages\pandas\core\generic.pyc in nonzero(self) 915 raise ValueError("The truth value of a {0} is ambiguous. " 916 "Use a.empty, a.bool(), a.item(), a.any() or a.all()." --> 917 .format(self.class.name)) 918 919 bool = nonzero

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()."

I tried everything I could but I was unable to solve it.

Thanks for the help!

ghost avatar Jun 07 '17 16:06 ghost

Can you open a new issue?

gusgordon avatar Jun 08 '17 14:06 gusgordon

Same issue with @gusgordon. Alphalens have implemented this feature but not pyfolio

huaiweicheng avatar Aug 09 '19 06:08 huaiweicheng