Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Portfolio Optimizer: Moving DateRangeSlider Key Error #394

Open
jtao1 opened this issue May 28, 2024 · 5 comments
Open

Portfolio Optimizer: Moving DateRangeSlider Key Error #394

jtao1 opened this issue May 28, 2024 · 5 comments

Comments

@jtao1
Copy link
Collaborator

jtao1 commented May 28, 2024

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
Copy link
Contributor

droumis commented May 29, 2024

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
Copy link
Collaborator Author

jtao1 commented Jun 4, 2024

Traceback
```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
Copy link
Collaborator Author

jtao1 commented Jun 4, 2024

Code to reproduce error

minimal reproducible example
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
Copy link
Contributor

droumis commented Jun 5, 2024

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

@maximlt
Copy link
Contributor

maximlt commented Jun 12, 2024

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants