Repository: spyder-ide/spyder

Issue:
TypeError when getting background color - DataFrameEditor
## Description

### What steps will reproduce the problem?

<!--- You can use Markdown here --->

C:\Users\dehaa\AppData\Local\Programs\Spyder jhbjj

### Traceback
```python-traceback
Traceback (most recent call last):
  File "C:\Users\dehaa\AppData\Local\Programs\Spyder\pkgs\spyder\plugins\variableexplorer\widgets\dataframeeditor.py", line 362, in data
    return to_qvariant(self.get_bgcolor(index))
  File "C:\Users\dehaa\AppData\Local\Programs\Spyder\pkgs\spyder\plugins\variableexplorer\widgets\dataframeeditor.py", line 308, in get_bgcolor
    if vmax - vmin == 0:
TypeError: unsupported operand type(s) for -: 'str' and 'str'
```

## Versions

* Spyder version: 5.3.1 
* Python version: 3.8.10
* Qt version: 5.15.2
* PyQt5 version: 5.15.6
* Operating System: Windows 10

### Dependencies

```
# Mandatory:
atomicwrites >=1.2.0                 :  1.4.0 (OK)
chardet >=2.0.0                      :  4.0.0 (OK)
cloudpickle >=0.5.0                  :  2.1.0 (OK)
cookiecutter >=1.6.0                 :  1.7.3 (OK)
diff_match_patch >=20181111          :  20200713 (OK)
intervaltree                         :  None (OK)
IPython >=7.31.1;<8.0.0              :  7.33.0 (OK)
jedi >=0.17.2;<0.19.0                :  0.18.1 (OK)
jellyfish >=0.7                      :  0.9.0 (OK)
jsonschema >=3.2.0                   :  4.5.1 (OK)
keyring >=17.0.0                     :  23.5.1 (OK)
nbconvert >=4.0                      :  6.5.0 (OK)
numpydoc >=0.6.0                     :  1.3.1 (OK)
paramiko >=2.4.0                     :  2.11.0 (OK)
parso >=0.7.0;<0.9.0                 :  0.8.3 (OK)
pexpect >=4.4.0                      :  4.8.0 (OK)
pickleshare >=0.4                    :  0.7.5 (OK)
psutil >=5.3                         :  5.9.1 (OK)
pygments >=2.0                       :  2.12.0 (OK)
pylint >=2.5.0                       :  2.13.9 (OK)
pyls_spyder >=0.4.0                  :  0.4.0 (OK)
pylsp >=1.4.1;<1.5.0                 :  1.4.1 (OK)
pylsp_black >=1.2.0                  :  1.2.1 (OK)
qdarkstyle >=3.0.2;<3.1.0            :  3.0.3 (OK)
qstylizer >=0.1.10                   :  0.2.1 (OK)
qtawesome >=1.0.2                    :  1.1.1 (OK)
qtconsole >=5.3.0;<5.4.0             :  5.3.0 (OK)
qtpy >=2.1.0                         :  2.1.0 (OK)
rtree >=0.9.7                        :  1.0.0 (OK)
setuptools >=49.6.0                  :  62.3.2 (OK)
sphinx >=0.6.6                       :  4.5.0 (OK)
spyder_kernels >=2.3.1;<2.4.0        :  2.3.1 (OK)
textdistance >=4.2.0                 :  4.2.2 (OK)
three_merge >=0.1.1                  :  0.1.1 (OK)
watchdog                             :  2.1.8 (OK)
zmq >=22.1.0                         :  23.0.0 (OK)

# Optional:
cython >=0.21                        :  0.29.30 (OK)
matplotlib >=3.0.0                   :  None (OK)
numpy >=1.7                          :  None (OK)
pandas >=1.1.1                       :  None (OK)
scipy >=0.17.0                       :  None (OK)
sympy >=0.7.3                        :  1.10.1 (OK)

# Spyder plugins:
spyder_terminal.terminalplugin 1.2.2 :  1.2.2 (OK)
```



Discussion:
Hi @akhilsharma2531! Could you share with us an example code to reproduce this in our side? Let us know!
@dalthviz, this is quite similar to issue #17145, which can be fixed by catching the `TypeError` and returning, instead of trying to compute the background color.

I'll open a PR to do that.

Resolving patch:
diff --git a/spyder/plugins/variableexplorer/widgets/dataframeeditor.py b/spyder/plugins/variableexplorer/widgets/dataframeeditor.py
--- a/spyder/plugins/variableexplorer/widgets/dataframeeditor.py
+++ b/spyder/plugins/variableexplorer/widgets/dataframeeditor.py
@@ -290,8 +290,10 @@ def colum_avg(self, state):
     def get_bgcolor(self, index):
         """Background color depending on value."""
         column = index.column()
+
         if not self.bgcolor_enabled:
             return
+
         value = self.get_value(index.row(), column)
         if self.max_min_col[column] is None or pd.isna(value):
             color = QColor(BACKGROUND_NONNUMBER_COLOR)
@@ -305,10 +307,18 @@ def get_bgcolor(self, index):
             else:
                 color_func = float
             vmax, vmin = self.return_max(self.max_min_col, column)
-            if vmax - vmin == 0:
-                vmax_vmin_diff = 1.0
-            else:
-                vmax_vmin_diff = vmax - vmin
+
+            # This is necessary to catch an error in Pandas when computing
+            # the difference between the max and min of a column.
+            # Fixes spyder-ide/spyder#18005
+            try:
+                if vmax - vmin == 0:
+                    vmax_vmin_diff = 1.0
+                else:
+                    vmax_vmin_diff = vmax - vmin
+            except TypeError:
+                return
+
             hue = (BACKGROUND_NUMBER_MINHUE + BACKGROUND_NUMBER_HUERANGE *
                    (vmax - color_func(value)) / (vmax_vmin_diff))
             hue = float(abs(hue))
@@ -317,6 +327,7 @@ def get_bgcolor(self, index):
             color = QColor.fromHsvF(hue, BACKGROUND_NUMBER_SATURATION,
                                     BACKGROUND_NUMBER_VALUE,
                                     BACKGROUND_NUMBER_ALPHA)
+
         return color
 
     def get_value(self, row, column):


Test patch: