pyfolio icon indicating copy to clipboard operation
pyfolio copied to clipboard

nested renamer is not supported

Open wukan1986 opened this issue 5 years ago • 3 comments

Round Trip Tear Sheet Example

pf.create_round_trip_tear_sheet(returns, positions, transactions)

SpecificationError Traceback (most recent call last) in ----> 1 pf.create_round_trip_tear_sheet(returns, positions, transactions)

d:\Users\Kan\Anaconda3\lib\site-packages\pyfolio\plotting.py in call_w_context(*args, **kwargs) 50 if set_context: 51 with plotting_context(), axes_style(): ---> 52 return func(*args, **kwargs) 53 else: 54 return func(*args, **kwargs)

d:\Users\Kan\Anaconda3\lib\site-packages\pyfolio\tears.py in create_round_trip_tear_sheet(returns, positions, transactions, sector_mappings, estimate_intraday, return_fig) 826 return 827 --> 828 round_trips.print_round_trip_stats(trades) 829 830 plotting.show_profit_attribution(trades)

d:\Users\Kan\Anaconda3\lib\site-packages\pyfolio\round_trips.py in print_round_trip_stats(round_trips, hide_pos) 399 """ 400 --> 401 stats = gen_round_trip_stats(round_trips) 402 403 print_table(stats['summary'], float_format='{:.2f}'.format,

d:\Users\Kan\Anaconda3\lib\site-packages\pyfolio\round_trips.py in gen_round_trip_stats(round_trips) 370 371 stats = {} --> 372 stats['pnl'] = agg_all_long_short(round_trips, 'pnl', PNL_STATS) 373 stats['summary'] = agg_all_long_short(round_trips, 'pnl', 374 SUMMARY_STATS)

d:\Users\Kan\Anaconda3\lib\site-packages\pyfolio\round_trips.py in agg_all_long_short(round_trips, col, stats_dict) 78 .assign(ones=1) 79 .groupby('ones')[col] ---> 80 .agg(stats_dict) 81 .T 82 .rename(columns={1.0: 'All trades'}))

d:\Users\Kan\Anaconda3\lib\site-packages\pandas\core\groupby\generic.py in aggregate(self, func, *args, **kwargs) 251 # but not the class list / tuple itself. 252 func = _maybe_mangle_lambdas(func) --> 253 ret = self._aggregate_multiple_funcs(func) 254 if relabeling: 255 ret.columns = columns

d:\Users\Kan\Anaconda3\lib\site-packages\pandas\core\groupby\generic.py in _aggregate_multiple_funcs(self, arg) 292 # GH 15931 293 if isinstance(self._selected_obj, Series): --> 294 raise SpecificationError("nested renamer is not supported") 295 296 columns = list(arg.keys())

SpecificationError: nested renamer is not supported

pandas:1.0.3

wukan1986 avatar Sep 09 '20 07:09 wukan1986

If I modify some code, will get other error.

` pf.round_trips.PNL_STATS = [(k,v) for k,v in pf.round_trips.PNL_STATS.items()] pf.round_trips.SUMMARY_STATS = [(k,v) for k,v in pf.round_trips.SUMMARY_STATS.items()] pf.round_trips.RETURN_STATS = [(k,v) for k,v in pf.round_trips.RETURN_STATS.items()] pf.round_trips.DURATION_STATS = [(k,v) for k,v in pf.round_trips.DURATION_STATS.items()]

pf.create_round_trip_tear_sheet(returns, positions, transactions) `


TypeError Traceback (most recent call last) in ----> 1 pf.create_round_trip_tear_sheet(returns, positions, transactions)

d:\Users\Kan\Anaconda3\lib\site-packages\pyfolio\plotting.py in call_w_context(*args, **kwargs) 50 if set_context: 51 with plotting_context(), axes_style(): ---> 52 return func(*args, **kwargs) 53 else: 54 return func(*args, **kwargs)

d:\Users\Kan\Anaconda3\lib\site-packages\pyfolio\tears.py in create_round_trip_tear_sheet(returns, positions, transactions, sector_mappings, estimate_intraday, return_fig) 845 ax_pnl_per_round_trip_pct = plt.subplot(gs[2, 1]) 846 --> 847 plotting.plot_round_trip_lifetimes(trades, ax=ax_trade_lifetimes) 848 849 plotting.plot_prob_profit_trade(trades, ax=ax_prob_profit_trade)

d:\Users\Kan\Anaconda3\lib\site-packages\pyfolio\plotting.py in plot_round_trip_lifetimes(round_trips, disp_amount, lsize, ax) 1768 ax.plot([row['open_dt'], row['close_dt']], 1769 [y_ix, y_ix], color=c, -> 1770 linewidth=lsize, solid_capstyle='butt') 1771 1772 ax.set_yticks(range(disp_amount))

d:\Users\Kan\Anaconda3\lib\site-packages\matplotlib\axes_axes.py in plot(self, scalex, scaley, data, *args, **kwargs) 1665 lines = [*self._get_lines(*args, data=data, **kwargs)] 1666 for line in lines: -> 1667 self.add_line(line) 1668 self.autoscale_view(scalex=scalex, scaley=scaley) 1669 return lines

d:\Users\Kan\Anaconda3\lib\site-packages\matplotlib\axes_base.py in add_line(self, line) 1900 line.set_clip_path(self.patch) 1901 -> 1902 self._update_line_limits(line) 1903 if not line.get_label(): 1904 line.set_label('_line%d' % len(self.lines))

d:\Users\Kan\Anaconda3\lib\site-packages\matplotlib\axes_base.py in _update_line_limits(self, line) 1922 Figures out the data limit of the given line, updating self.dataLim. 1923 """ -> 1924 path = line.get_path() 1925 if path.vertices.size == 0: 1926 return

d:\Users\Kan\Anaconda3\lib\site-packages\matplotlib\lines.py in get_path(self) 1025 """ 1026 if self._invalidy or self._invalidx: -> 1027 self.recache() 1028 return self._path 1029

d:\Users\Kan\Anaconda3\lib\site-packages\matplotlib\lines.py in recache(self, always) 668 if always or self._invalidx: 669 xconv = self.convert_xunits(self._xorig) --> 670 x = _to_unmasked_float_array(xconv).ravel() 671 else: 672 x = self._x

d:\Users\Kan\Anaconda3\lib\site-packages\matplotlib\cbook_init_.py in _to_unmasked_float_array(x) 1388 return np.ma.asarray(x, float).filled(np.nan) 1389 else: -> 1390 return np.asarray(x, float) 1391 1392

d:\Users\Kan\Anaconda3\lib\site-packages\numpy\core_asarray.py in asarray(a, dtype, order) 83 84 """ ---> 85 return array(a, dtype, copy=False, order=order) 86 87

TypeError: float() argument must be a string or a number, not 'Timestamp'

matplotlib:3.1.3 pandas: 1.0.3 python:3.7.3

wukan1986 avatar Sep 09 '20 07:09 wukan1986

The commit of keli above solved this problem.

wuzhe1234 avatar Jul 14 '21 13:07 wuzhe1234

Please create the PR for the commit. The commit fixed the problem for me as well.

Rajmehta123 avatar Aug 03 '21 17:08 Rajmehta123