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

Tabulator: Invalid patch index error when calling patch with non-standard pandas indices #5056

Closed
gorj-tessella opened this issue Jun 6, 2023 · 2 comments
Labels
type: bug Something isn't correct or isn't working
Milestone

Comments

@gorj-tessella
Copy link

Tabulator version info

1.0.4

Description of expected behavior and the observed behavior

On a dataframe with non-standard index (ordered integer starting at zero), patch index should be applied but fails with ValueError: Invalid patch index

I believe the bug is here, where as_index is used to interpret the row index in patch_value as actual value or position when updating the underlying dataframe, but then patch_value is passed to the underlying _patch without modifcation. The downstream code appears to require positional information, not named index.

for k, v in patch_value.items():
for (ind, value) in v:
if isinstance(ind, slice):
ind = range(ind.start, ind.stop, ind.step or 1)
if as_index:
self.value.loc[ind, k] = value
else:
self.value.iloc[ind, columns.index(k)] = value
self._patch(patch_value)

Complete, minimal, self-contained example code that reproduces the issue

In a jupyter notebook:

First cell:

from panel.widgets import Tabulator
import pandas as pd

df1 = pd.DataFrame([1, 2, 3], index=["a", "b", "c"], columns=["x"])
widget = Tabulator(df1)
widget

Second cell (works):

widget.patch({'x':[(0, 5)]}, as_index=False)

Third cell (fails with error below):

widget.patch({'x':[('a', 10)]})

Stack traceback and/or browser JavaScript console output


ValueError Traceback (most recent call last)
Cell In[73], line 1
----> 1 widget.patch({'x':[('a', 10)]})

File /opt/miniconda3/envs/ais/lib/python3.9/site-packages/panel/widgets/tables.py:815, in BaseTable.patch(self, patch_value, as_index)
813 else:
814 self.value.iloc[ind, columns.index(k)] = value
--> 815 self._patch(patch_value)
816 else:
817 raise ValueError(
818 f"Patching with a patch_value of type {type(patch_value).name} "
819 "is not supported. Please provide a DataFrame, Series or Dict."
820 )

File /opt/miniconda3/envs/ais/lib/python3.9/site-packages/panel/util/init.py:372, in updating..wrapped(self, *args, **kwargs)
370 self._updating = True
371 try:
--> 372 fn(self, *args, **kwargs)
373 finally:
374 self._updating = updating

File /opt/miniconda3/envs/ais/lib/python3.9/site-packages/panel/widgets/tables.py:1443, in Tabulator._patch(self, patch)
1441 if not patch:
1442 return
-> 1443 super()._patch(patch)
1444 self._update_style()
1445 self._update_selectable()

File /opt/miniconda3/envs/ais/lib/python3.9/site-packages/panel/util/init.py:372, in updating..wrapped(self, *args, **kwargs)
370 self._updating = True
371 try:
--> 372 fn(self, *args, **kwargs)
373 finally:
374 self._updating = updating

File /opt/miniconda3/envs/ais/lib/python3.9/site-packages/panel/reactive.py:1035, in SyncableData._patch(self, patch)
1033 if comm or not doc.session_context or state._unblocked(doc):
1034 with unlocked():
-> 1035 m.source.patch(patch)
1036 if comm and 'embedded' not in root.tags:
1037 push(doc, comm)

File /opt/miniconda3/envs/ais/lib/python3.9/site-packages/bokeh/models/sources.py:723, in ColumnDataSource.patch(self, patches, setter)
720 _check_slice(subind)
722 else:
--> 723 raise ValueError("Invalid patch index: %s" % ind)
725 self.data._patch(self.document, self, patches, setter)

ValueError: Invalid patch index: a

Screenshots or screencasts of the bug in action

@philippjfr philippjfr added the type: bug Something isn't correct or isn't working label Jun 7, 2023
@philippjfr philippjfr added this to the v1.1.1 milestone Jun 7, 2023
@gorj-tessella
Copy link
Author

Possibly this is related to the following code section; although Tabulator has a filters param, it also has a _filters attribute which is the one that appears to be used throughout the code. Thus even when you have done add_filter this section is skipped.

panel/panel/widgets/tables.py

Lines 1432 to 1435 in 60ba9c9

if self.filters or self.sorters:
self._updating = False
self._update_cds()
return

@philippjfr philippjfr modified the milestones: v1.1.1, v1.2.0 Jun 22, 2023
@philippjfr philippjfr modified the milestones: v1.2.0, v1.2.1 Jul 10, 2023
@philippjfr philippjfr modified the milestones: v1.2.1, v1.2.2 Jul 25, 2023
@hoxbro hoxbro modified the milestones: v1.2.2, v1.2.3 Sep 4, 2023
@philippjfr philippjfr modified the milestones: v1.2.3, v1.2.4 Sep 18, 2023
@philippjfr philippjfr modified the milestones: v1.2.4, v1.3.0 Oct 9, 2023
@philippjfr
Copy link
Member

Fixed by #5645

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't correct or isn't working
Projects
None yet
Development

No branches or pull requests

3 participants