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

selected_dataframe is linked to the current_view #3670

Open
maximlt opened this issue Jul 4, 2022 · 2 comments · May be fixed by #6685
Open

selected_dataframe is linked to the current_view #3670

maximlt opened this issue Jul 4, 2022 · 2 comments · May be fixed by #6685
Labels
component: tabulator Related to the Tabulator widget type: bug Something isn't correct or isn't working
Milestone

Comments

@maximlt
Copy link
Member

maximlt commented Jul 4, 2022

The selected_dataframe property is computed based on the current_view, which can return wrong data or raise an error if some filters are applied. I think the selected_dataframe should not be linked to the current_view, it seems useful in a large table to be able to filter (and sort) and select rows at the same time.

@maximlt maximlt added type: bug Something isn't correct or isn't working component: tabulator Related to the Tabulator widget labels Jul 4, 2022
@peterroelants
Copy link

I think I just run into this because selected_dataframe returns self.current_view.iloc[self.selection], and self.selection indexs the original dataframe and not self.current_view.

File [~/mambaforge/envs/lcms_polymer_env/lib/python3.11/site-packages/panel/widgets/tables.py:839](https://file+.vscode-resource.vscode-cdn.net/home/peter/workspace/lcms_polymer_model/notebooks/peter/aspect_platform/apps/~/mambaforge/envs/lcms_polymer_env/lib/python3.11/site-packages/panel/widgets/tables.py:839), in BaseTable.selected_dataframe(self)
    837 if not self.selection:
    838     return self.current_view.iloc[:0]
--> 839 return self.current_view.iloc[self.selection]

File [~/mambaforge/envs/lcms_polymer_env/lib/python3.11/site-packages/pandas/core/indexing.py:1073](https://file+.vscode-resource.vscode-cdn.net/home/peter/workspace/lcms_polymer_model/notebooks/peter/aspect_platform/apps/~/mambaforge/envs/lcms_polymer_env/lib/python3.11/site-packages/pandas/core/indexing.py:1073), in _LocationIndexer.__getitem__(self, key)
   1070 axis = self.axis or 0
   1072 maybe_callable = com.apply_if_callable(key, self.obj)
-> 1073 return self._getitem_axis(maybe_callable, axis=axis)

File [~/mambaforge/envs/lcms_polymer_env/lib/python3.11/site-packages/pandas/core/indexing.py:1616](https://file+.vscode-resource.vscode-cdn.net/home/peter/workspace/lcms_polymer_model/notebooks/peter/aspect_platform/apps/~/mambaforge/envs/lcms_polymer_env/lib/python3.11/site-packages/pandas/core/indexing.py:1616), in _iLocIndexer._getitem_axis(self, key, axis)
   1614 # a list of integers
   1615 elif is_list_like_indexer(key):
-> 1616     return self._get_list_axis(key, axis=axis)
   1618 # a single integer
   1619 else:
   1620     key = item_from_zerodim(key)

File [~/mambaforge/envs/lcms_polymer_env/lib/python3.11/site-packages/pandas/core/indexing.py:1590](https://file+.vscode-resource.vscode-cdn.net/home/peter/workspace/lcms_polymer_model/notebooks/peter/aspect_platform/apps/~/mambaforge/envs/lcms_polymer_env/lib/python3.11/site-packages/pandas/core/indexing.py:1590), in _iLocIndexer._get_list_axis(self, key, axis)
   1587     return self.obj._take_with_is_copy(key, axis=axis)
   1588 except IndexError as err:
   1589     # re-raise with different error message
-> 1590     raise IndexError("positional indexers are out-of-bounds") from err

IndexError: positional indexers are out-of-bounds

@MarcSkovMadsen
Copy link
Collaborator

MarcSkovMadsen commented Feb 27, 2024

To understand what happens you can play with the code below.

import panel as pn
import pandas as pd
import random
from datetime import datetime, timedelta

pn.extension("tabulator", template="fast")

df = pd.DataFrame({
    "x": [1,3,2],
    "y": ["a", "b", "c"]
})

table = pn.widgets.Tabulator(df, sizing_mode="stretch_both", selectable='checkbox', height=300)
update = pn.widgets.Button(name="Update")

@pn.depends(update)
def result(event):
    iloc = df.iloc[table.selection,:]
    loc = df.loc[table.selection,:]

    return pn.Row(
        pn.Column("df", df), 
        pn.Column("table.current_view", table.current_view),
        pn.Column("table.selection", table.selection),
        pn.Column("selected_dataframe", table.selected_dataframe), 
        pn.Column("df.iloc", iloc), 
        pn.Column("df.loc", loc),
        pn.Column("table.current_view.iloc", table.current_view.iloc[table.selection,:]), 
        pn.Column("table.current_view.loc", table.current_view.loc[table.selection,:]),
    )

pn.Column(update, table, result).servable()

As long as the user has not sorted Tabulator things works.

image

But if we sort (on column x) things gets out of sync.

image

I think was it not clearly documented is that selection returns the selected indicies from the index.

image

At least for our use case using df.loc[table.selection] instead of table.selected_dataframe will work.

@philippjfr philippjfr modified the milestones: v1.4.1, v1.4.2 Apr 8, 2024
@philippjfr philippjfr modified the milestones: v1.4.2, v1.4.3 Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: tabulator Related to the Tabulator widget type: bug Something isn't correct or isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants