vectorbt icon indicating copy to clipboard operation
vectorbt copied to clipboard

Pandas MultiIndex on axis 0

Open sergejlazuk opened this issue 1 year ago • 1 comments

MultiIndex on columns (axis1) is supported well - inputs get matched on column indexes, e.g. prices and signals frames are alligned. However, rows MultiIndex (axis0) is not working as expected: when I try to use prices and signals DataFrames with rows MultiIndex (e.g. levels “Ticker“ and „Timestamp“ instead of just „Timestamp“) I get an error: „Broadcasting index is not allowed when index_from =strict“. It works if one sorts the DataFrames beforehand, e.g.

df_prices=df_prices.sort_index() df_signals=df_signals.sort_index()

Would be nice if row MultiIndexes would be treated same as columns MultiIndexes - inputs alligned by row index (and broadcasted to same shape if needed).

And then, when plotting results with MultiIndex rows, would be nice to have all index levels shown hierarchically on x-axis, e.g. like this:

import plotly.graph_objs as go fig = go.Figure() fig.add_trace(go.Scatter(x=[df.index.get_level_values('asset'),df.index.get_level_values('DT')], y=df['fwd'])) fig.show() rowsMultiIndex

Currently vbt plot produces this (Indexes collapsed into single label): asset_values

pf.value().vbt.plot()

rowsMultiIndexNow

sergejlazuk avatar Jul 08 '24 15:07 sergejlazuk

Currently, index is assumed to be time and thus sorted. But it's an interesting idea, thank you for your suggestion. I've put it to the features-wanted list.

polakowo avatar Jul 20 '24 21:07 polakowo