Tabulator selection is based on original index order, not what you see after sorting by column. In the example I sorted by column ‘a’, then selected row with index ‘i’. Selection is based on original order and row ‘k’ is selected.
Welcome to the community @hoseppan
This definitely is a bug. You should report it on GitHub.
import panel as pn
import pandas as pd
pn.extension("tabulator")
df = pd.DataFrame({str(n): [1,2,3] for n, k in enumerate("abc")}, index=list("ijk"))
tab = pn.widgets.Tabulator(
df,
pagination="remote",
page_size=5,
selectable="checkbox"
)
The bug seems to be related to the pagination="remote"
option, as it seem fine when I comment out that line.
I also noticed with my code example, I get the following error when trying to sort by the index column. This error also happens when the column names are integers instead of strings.
Traceback (most recent call last):
File "/home/shh/miniconda3/envs/holoviz/lib/python3.8/site-packages/pyviz_comms/__init__.py", line 325, in _handle_msg
self._on_msg(msg)
File "/home/shh/Development/holoviz/panel/panel/viewable.py", line 273, in _on_msg
doc.unhold()
File "/home/shh/miniconda3/envs/holoviz/lib/python3.8/site-packages/bokeh/document/document.py", line 799, in unhold
self.callbacks.unhold()
File "/home/shh/miniconda3/envs/holoviz/lib/python3.8/site-packages/bokeh/document/callbacks.py", line 396, in unhold
self.trigger_on_change(event)
File "/home/shh/miniconda3/envs/holoviz/lib/python3.8/site-packages/bokeh/document/callbacks.py", line 373, in trigger_on_change
invoke_with_curdoc(doc, event.callback_invoker)
File "/home/shh/miniconda3/envs/holoviz/lib/python3.8/site-packages/bokeh/document/callbacks.py", line 408, in invoke_with_curdoc
return f()
File "/home/shh/miniconda3/envs/holoviz/lib/python3.8/site-packages/bokeh/util/callback_manager.py", line 191, in invoke
callback(attr, old, new)
File "/home/shh/Development/holoviz/panel/panel/reactive.py", line 319, in _comm_change
self._schedule_change(doc, comm)
File "/home/shh/Development/holoviz/panel/panel/reactive.py", line 306, in _schedule_change
self._change_event(doc)
File "/home/shh/Development/holoviz/panel/panel/reactive.py", line 300, in _change_event
self._process_events(events)
File "/home/shh/Development/holoviz/panel/panel/reactive.py", line 1056, in _process_events
super(ReactiveData, self)._process_events(events)
File "/home/shh/Development/holoviz/panel/panel/reactive.py", line 266, in _process_events
self.param.update(**self_events)
File "/home/shh/miniconda3/envs/holoviz/lib/python3.8/site-packages/param/parameterized.py", line 1877, in update
self_._batch_call_watchers()
File "/home/shh/miniconda3/envs/holoviz/lib/python3.8/site-packages/param/parameterized.py", line 2038, in _batch_call_watchers
self_._execute_watcher(watcher, events)
File "/home/shh/miniconda3/envs/holoviz/lib/python3.8/site-packages/param/parameterized.py", line 2000, in _execute_watcher
watcher.fn(*args, **kwargs)
File "/home/shh/Development/holoviz/panel/panel/widgets/tables.py", line 1202, in _update_cds
super()._update_cds(*events)
File "/home/shh/Development/holoviz/panel/panel/util.py", line 368, in wrapped
fn(self, *args, **kwargs)
File "/home/shh/Development/holoviz/panel/panel/widgets/tables.py", line 201, in _update_cds
self._processed, data = self._get_data()
File "/home/shh/Development/holoviz/panel/panel/widgets/tables.py", line 1032, in _get_data
df = self._sort_df(df)
File "/home/shh/Development/holoviz/panel/panel/widgets/tables.py", line 1022, in _sort_df
return df.sort_values(
File "/home/shh/miniconda3/envs/holoviz/lib/python3.8/site-packages/pandas/util/_decorators.py", line 311, in wrapper
return func(*args, **kwargs)
File "/home/shh/miniconda3/envs/holoviz/lib/python3.8/site-packages/pandas/core/frame.py", line 6259, in sort_values
k = self._get_label_or_level_values(by, axis=axis)
File "/home/shh/miniconda3/envs/holoviz/lib/python3.8/site-packages/pandas/core/generic.py", line 1779, in _get_label_or_level_values
raise KeyError(key)
KeyError: 'index'
I hit the same bug but work around was to use:
tab.value.iloc[tab.selection]
Seems the obj.selected_dataframe is returning the following:
tab.current_view.iloc[tab.selection]
WARNING: If you add filters to the Tabulator it also has further indexing issues.
2 Likes