From b6f31afdda3c84832f63263f6507ae37ebcf6905 Mon Sep 17 00:00:00 2001 From: Dieter Weber Date: Wed, 22 Oct 2025 13:39:07 +0200 Subject: [PATCH] Don't error out when emptying a dataframe For some reason a spurious selection update event is processed after the data frame has been emptied, resulting in an IndexError. With this change an empty selection is created in case any of the selected indices are invalid. --- panel/widgets/tables.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/panel/widgets/tables.py b/panel/widgets/tables.py index 098a541664..21152aac2e 100644 --- a/panel/widgets/tables.py +++ b/panel/widgets/tables.py @@ -1844,7 +1844,10 @@ def _map_indexes(self, indexes: list[int], existing: list[int] = [], add: bool = else: start = 0 ilocs = list(existing) - index = self._processed.iloc[[start+ind for ind in indexes]].index + try: + index = self._processed.iloc[[start+ind for ind in indexes]].index + except IndexError: + index = self._processed.iloc[[]].index for v in index.values: try: iloc = self.value.index.get_loc(v) @@ -1869,7 +1872,6 @@ def _update_selection(self, indices: list[int] | SelectionEvent): selected = indices.selected ilocs = [] if indices.flush else self.selection.copy() inds = indices.indices - ilocs = self._map_indexes(inds, ilocs, add=selected) if isinstance(self.selectable, int) and not isinstance(self.selectable, bool): ilocs = ilocs[len(ilocs) - self.selectable:]