holoviz-topics / examples

Visualization-focused examples using HoloViz for specific topics
https://examples.holoviz.org
Creative Commons Attribution 4.0 International
84 stars 25 forks source link

Portfolio Optimizer: Moving DateRangeSlider Key Error #394

Open jtao1 opened 6 months ago

jtao1 commented 6 months ago

Moving the date range slider is intended to change the x-axis date range on the performance graph.

year = pn.widgets.DateRangeSlider(name='Year', value=(stocks.index.rx.value.min(), stocks.index.rx.value.max()), \ start=stocks.index.min(), end=stocks.index.max())

However when moving the slider around in the notebook it creates a KeyError and the performance graph does not change. Interestingly, when running the dashboard, the slider does work as intended.

Untitled video - Made with Clipchamp

droumis commented 6 months ago

Please provide code for a minimal reproducible example that demonstrates the error. Also post the full traceback error you see. Also please provide the output of hv.show_versions()

jtao1 commented 5 months ago
Traceback ```python ```Traceback (most recent call last): File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/pyviz_comms/__init__.py", line 340, in _handle_msg self._on_msg(msg) File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/panel/viewable.py", line 479, in _on_msg doc.unhold() File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/bokeh/document/document.py", line 776, in unhold self.callbacks.unhold() File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/bokeh/document/callbacks.py", line 431, in unhold self.trigger_on_change(event) File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/bokeh/document/callbacks.py", line 408, in trigger_on_change invoke_with_curdoc(doc, event.callback_invoker) File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/bokeh/document/callbacks.py", line 443, in invoke_with_curdoc return f() ^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/bokeh/util/callback_manager.py", line 185, in invoke callback(attr, old, new) File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/panel/reactive.py", line 475, in _comm_change state._handle_exception(e) File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/panel/io/state.py", line 458, in _handle_exception raise exception File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/panel/reactive.py", line 473, in _comm_change self._schedule_change(doc, comm) File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/panel/reactive.py", line 455, in _schedule_change self._change_event(doc) File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/panel/reactive.py", line 451, in _change_event self._process_events(events) File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/panel/reactive.py", line 388, in _process_events self.param.update(**self_params) File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/parameterized.py", line 2318, in update restore = dict(self_._update(arg, **kwargs)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/parameterized.py", line 2351, in _update self_._batch_call_watchers() File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/parameterized.py", line 2545, in _batch_call_watchers self_._execute_watcher(watcher, events) File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/parameterized.py", line 2505, in _execute_watcher watcher.fn(*args, **kwargs) File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/parameterized.py", line 784, in _sync_caller return function() ^^^^^^^^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/depends.py", line 53, in _depends return func(*args, **kw) ^^^^^^^^^^^^^^^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/panel/widgets/slider.py", line 742, in _sync_values self.param.update(value_start=vs, value_end=ve) File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/parameterized.py", line 2318, in update restore = dict(self_._update(arg, **kwargs)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/parameterized.py", line 2351, in _update self_._batch_call_watchers() File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/parameterized.py", line 2545, in _batch_call_watchers self_._execute_watcher(watcher, events) File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/parameterized.py", line 2505, in _execute_watcher watcher.fn(*args, **kwargs) File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/panel/param.py", line 883, in _replace_pane new_object = self.eval(self.object) ^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/panel/param.py", line 1106, in eval return eval_function_with_deps(ref) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/parameterized.py", line 165, in eval_function_with_deps return function(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/depends.py", line 53, in _depends return func(*args, **kw) ^^^^^^^^^^^^^^^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/reactive.py", line 594, in wrapped return eval_fn()(*combined_args, **combined_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/tmp/ipykernel_40708/641140117.py", line 58, in plot_performance price_on_start_date = selected_stocks.loc[value_start].iloc[0] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/reactive.py", line 1013, in __getattribute__ self._resolve() File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/reactive.py", line 921, in _resolve raise e File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/reactive.py", line 910, in _resolve obj = self._eval_operation(obj, operation) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/reactive.py", line 1215, in _eval_operation obj = fn(obj, *resolved_args, **resolved_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/pandas/core/indexing.py", line 1191, in __getitem__ return self._getitem_axis(maybe_callable, axis=axis) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/pandas/core/indexing.py", line 1431, in _getitem_axis return self._get_label(key, axis=axis) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/pandas/core/indexing.py", line 1381, in _get_label return self.obj.xs(label, axis=axis) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/pandas/core/generic.py", line 4298, in xs loc = index.get_loc(key) ^^^^^^^^^^^^^^^^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/pandas/core/indexes/datetimes.py", line 627, in get_loc raise KeyError(key) KeyError: datetime.date(2012, 1, 3) Traceback (most recent call last): File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/pyviz_comms/__init__.py", line 340, in _handle_msg self._on_msg(msg) File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/panel/viewable.py", line 479, in _on_msg doc.unhold() File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/bokeh/document/document.py", line 776, in unhold self.callbacks.unhold() File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/bokeh/document/callbacks.py", line 431, in unhold self.trigger_on_change(event) File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/bokeh/document/callbacks.py", line 408, in trigger_on_change invoke_with_curdoc(doc, event.callback_invoker) File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/bokeh/document/callbacks.py", line 443, in invoke_with_curdoc return f() ^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/bokeh/util/callback_manager.py", line 185, in invoke callback(attr, old, new) File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/panel/reactive.py", line 475, in _comm_change state._handle_exception(e) File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/panel/io/state.py", line 458, in _handle_exception raise exception File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/panel/reactive.py", line 473, in _comm_change self._schedule_change(doc, comm) File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/panel/reactive.py", line 455, in _schedule_change self._change_event(doc) File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/panel/reactive.py", line 451, in _change_event self._process_events(events) File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/panel/reactive.py", line 388, in _process_events self.param.update(**self_params) File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/parameterized.py", line 2318, in update restore = dict(self_._update(arg, **kwargs)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/parameterized.py", line 2351, in _update self_._batch_call_watchers() File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/parameterized.py", line 2545, in _batch_call_watchers self_._execute_watcher(watcher, events) File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/parameterized.py", line 2505, in _execute_watcher watcher.fn(*args, **kwargs) File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/parameterized.py", line 784, in _sync_caller return function() ^^^^^^^^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/depends.py", line 53, in _depends return func(*args, **kw) ^^^^^^^^^^^^^^^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/panel/widgets/slider.py", line 742, in _sync_values self.param.update(value_start=vs, value_end=ve) File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/parameterized.py", line 2318, in update restore = dict(self_._update(arg, **kwargs)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/parameterized.py", line 2351, in _update self_._batch_call_watchers() File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/parameterized.py", line 2545, in _batch_call_watchers self_._execute_watcher(watcher, events) File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/parameterized.py", line 2505, in _execute_watcher watcher.fn(*args, **kwargs) File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/panel/param.py", line 883, in _replace_pane new_object = self.eval(self.object) ^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/panel/param.py", line 1106, in eval return eval_function_with_deps(ref) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/parameterized.py", line 165, in eval_function_with_deps return function(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/depends.py", line 53, in _depends return func(*args, **kw) ^^^^^^^^^^^^^^^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/reactive.py", line 594, in wrapped return eval_fn()(*combined_args, **combined_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/tmp/ipykernel_40708/641140117.py", line 58, in plot_performance price_on_start_date = selected_stocks.loc[value_start].iloc[0] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/reactive.py", line 1013, in __getattribute__ self._resolve() File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/reactive.py", line 921, in _resolve raise e File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/reactive.py", line 910, in _resolve obj = self._eval_operation(obj, operation) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/param/reactive.py", line 1215, in _eval_operation obj = fn(obj, *resolved_args, **resolved_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/pandas/core/indexing.py", line 1191, in __getitem__ return self._getitem_axis(maybe_callable, axis=axis) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/pandas/core/indexing.py", line 1431, in _getitem_axis return self._get_label(key, axis=axis) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/pandas/core/indexing.py", line 1381, in _get_label return self.obj.xs(label, axis=axis) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/pandas/core/generic.py", line 4298, in xs loc = index.get_loc(key) ^^^^^^^^^^^^^^^^^^ File "/mnt/c/Users/Impor/holoviz-examples/examples/portfolio_optimizer/envs/default/lib/python3.11/site-packages/pandas/core/indexes/datetimes.py", line 627, in get_loc raise KeyError(key) KeyError: datetime.date(2012, 1, 3)``` ```
jtao1 commented 5 months ago

Code to reproduce error

minimal reproducible example ```python import holoviews as hv from io import BytesIO import pandas as pd import hvplot.pandas # noqa import panel as pn pn.extension('tabulator', design='material', template='material', theme_toggle=True, loading_indicator=True) file_input = pn.widgets.FileInput(sizing_mode='stretch_width') def get_stocks(data): if data is None: stock_file = 'https://datasets.holoviz.org/stocks/v1/stocks.csv' else: stock_file = BytesIO(data) return pd.read_csv(stock_file, index_col='Date', parse_dates=True) stocks = hvplot.bind(get_stocks, file_input).interactive() stocks = pn.rx(get_stocks)(file_input) selector = pn.widgets.MultiSelect( name='Select stocks', sizing_mode='stretch_width', options=stocks.columns.to_list() ) posxy = hv.streams.Tap(x=None, y=None) sidebar = pn.layout.WidgetBox( file_input, selector, max_width=350, sizing_mode='stretch_width' ).servable(area='sidebar') selector = pn.widgets.MultiSelect( name='Select stocks', sizing_mode='stretch_width', options=stocks.columns.to_list() ) selected_stocks = stocks.rx.pipe( lambda df, cols: df[cols] if cols else df, selector ) year = pn.widgets.DateRangeSlider( name="Year", value=(stocks.index.rx.value.min(), stocks.index.rx.value.max()), start=stocks.index.min(), end=stocks.index.max(), ) def plot_performance(value_start, value_end): allocation = 1000 stocks_between_dates = selected_stocks.loc[value_start:value_end] price_on_start_date = selected_stocks.loc[value_start].iloc[0] plot = ( (stocks_between_dates * allocation / price_on_start_date) .sum(axis=1) .hvplot.line( ylabel="Total Value ($)", title="Portfolio performance", responsive=True, min_height=400, ) ) return plot performance_plot = pn.bind( plot_performance, year.param.value_start, year.param.value_end, ) performance = pn.Column(performance_plot, sizing_mode="stretch_both") pn.Column(sidebar, pn.Row(year, performance)) ```
droumis commented 5 months ago

observation by @jtao1 : error in notebook, not in served app

maximlt commented 5 months ago

@jtao1 I created a new environment from your branch () and copied the code you shared in this issue. I could not reproduce the issue you reported, see the screenshot below. Please spend some time again to reproduce the issue, and if you manage, report again how to reproduce.

cannot_repro