From 0be3f5cc8635359c67df25b6becedea575b1f865 Mon Sep 17 00:00:00 2001 From: Arthur Tavares Date: Fri, 25 Aug 2023 13:55:37 +0200 Subject: [PATCH] Include a check for the index's size before trying to access it This code checks whether col is less than the length of the columns or the index, depending on the orientation, before trying to access the value. If col is out of bounds, it will simply return None, avoiding the IndexError. --- tablexplore/core.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/tablexplore/core.py b/tablexplore/core.py index 39dcc7d..234e35d 100644 --- a/tablexplore/core.py +++ b/tablexplore/core.py @@ -2068,17 +2068,19 @@ def headerData(self, col, orientation, role): if role == QtCore.Qt.DisplayRole: if orientation == QtCore.Qt.Horizontal: - return str(self.df.columns[col]) - if orientation == QtCore.Qt.Vertical: - value = self.df.index[col] - if type( self.df.index) == pd.DatetimeIndex: - if not value is pd.NaT: - try: - return value.strftime(TIMEFORMAT) - except: - return '' - else: - return str(value) + if col < len(self.df.columns): + return str(self.df.columns[col]) + elif orientation == QtCore.Qt.Vertical: + if col < len(self.df.index): + value = self.df.index[col] + if type(self.df.index) == pd.DatetimeIndex: + if value is not pd.NaT: + try: + return value.strftime(TIMEFORMAT) + except: + return '' + else: + return str(value) return None def setData(self, index, value, role=QtCore.Qt.EditRole):