Repository: spyder-ide/spyder

Issue:
Variable Explorer bug when sorting by 'size'
<!--- **PLEASE READ:** When submitting here, please ensure you've completed the following checklist and checked the boxes to confirm. Issue reports without it may be closed. Thanks! --->

### Issue Report Checklist

* [ ] Searched the [issues page](https://github.com/spyder-ide/spyder/issues?q=is%3Aissue) for similar reports
* [x] Read the relevant sections of the [Spyder Troubleshooting Guide](https://github.com/spyder-ide/spyder/wiki/Troubleshooting-Guide-and-FAQ) and followed its advice
* [ ] Reproduced the issue after updating with ``conda update spyder`` (or ``pip``, if not using Anaconda)
* [ ] Could not reproduce inside ``jupyter qtconsole`` (if console-related)
* [ ] Tried basic troubleshooting (if a bug/error)
    * [ ] Restarted Spyder
    * [ ] Reset preferences with ``spyder --reset``
    * [ ] Reinstalled the latest version of [Anaconda](https://www.anaconda.com/download/)
    * [ ] Tried the other applicable steps from the Troubleshooting Guide
* [ ] Completed the **Problem Description**, **Steps to Reproduce** and **Version** sections below


## Problem Description
The variable explorer bugs when sorting variables by size.
![spider](https://user-images.githubusercontent.com/36287784/58977361-27d73880-878f-11e9-890f-9d0b15d58070.jpg)
Here I ran my script and the variable explorer shows all my variables correctly.
![spider 2](https://user-images.githubusercontent.com/36287784/58977409-4c331500-878f-11e9-8156-d873713af278.jpg)
Here, I sorted the variables by type and everything is still fine.
![spider 3](https://user-images.githubusercontent.com/36287784/58977441-5e14b800-878f-11e9-85f8-8d54cac8b056.jpg)
Here, I sorted the variables by size and the sorting is messed up.
As we can see in the first picture, my 'data' variable is a (32,117) Dataframe. On the second picture, everything is still ok, but in the third picture, when I sorted them by size, it shows that 'data' is (32,) size, while the correct size is attached to the 'conf_games' variable.



### What steps reproduce the problem?

1. Sort the variables by type and then by size.
2. 
3. 

### What is the expected output? What do you see instead?
Explained above.


### Paste Traceback/Error Below (if applicable)
<!--- Copy from error dialog or View > Panes > Internal Console --->

```python-traceback

PASTE TRACEBACK HERE

```

## Versions
<!--- You can get this information from Help > About Spyder...
or (if Spyder won't launch) the "conda list" command
from the Anaconda Prompt/Terminal/command line. --->

* Spyder version: 3.3.4
* Python version: 3.7.3
* Qt version: 5.9.2
* PyQt version: 5.9.6
* Operating System name/version: Windows 10

### Dependencies
<!--- Please go to the menu entry Help > Dependencies,
press the Copy to clipboard button and paste below --->

```
PASTE DEPENDENCIES HERE
```



Discussion:
Thanks for reporting. We'll try to solve this in Spyder 4.

Resolving patch:
diff --git a/spyder/plugins/variableexplorer/widgets/collectionseditor.py b/spyder/plugins/variableexplorer/widgets/collectionseditor.py
--- a/spyder/plugins/variableexplorer/widgets/collectionseditor.py
+++ b/spyder/plugins/variableexplorer/widgets/collectionseditor.py
@@ -376,8 +376,10 @@ def data(self, index, role=Qt.DisplayRole):
         else:
             if is_type_text_string(value):
                 display = to_text_string(value, encoding="utf-8")
-            else:
+            elif not isinstance(value, int):
                 display = to_text_string(value)
+            else:
+                display = value
         if role == Qt.DisplayRole:
             return to_qvariant(display)
         elif role == Qt.EditRole:


Test patch:
diff --git a/spyder/plugins/variableexplorer/widgets/tests/test_collectioneditor.py b/spyder/plugins/variableexplorer/widgets/tests/test_collectioneditor.py
--- a/spyder/plugins/variableexplorer/widgets/tests/test_collectioneditor.py
+++ b/spyder/plugins/variableexplorer/widgets/tests/test_collectioneditor.py
@@ -139,11 +139,11 @@ def test_collectionsmodel_with_two_ints():
         row_with_x = 1
         row_with_y = 0
     assert data(cm, row_with_x, 1) == 'int'
-    assert data(cm, row_with_x, 2) == '1'
+    assert data(cm, row_with_x, 2) == 1
     assert data(cm, row_with_x, 3) == '1'
     assert data(cm, row_with_y, 0) == 'y'
     assert data(cm, row_with_y, 1) == 'int'
-    assert data(cm, row_with_y, 2) == '1'
+    assert data(cm, row_with_y, 2) == 1
     assert data(cm, row_with_y, 3) == '2'
 
 def test_collectionsmodel_with_index():
@@ -175,34 +175,71 @@ def test_shows_dataframeeditor_when_editing_index(qtbot, monkeypatch):
 
 
 def test_sort_collectionsmodel():
+    var_list1 = [0, 1, 2]
+    var_list2 = [3, 4, 5, 6]
+    var_dataframe1 = pandas.DataFrame([[1, 2, 3], [20, 30, 40], [2, 2, 2]])
+    var_dataframe2 = pandas.DataFrame([[1, 2, 3], [20, 30, 40]])
+    var_series1 = pandas.Series(var_list1)
+    var_series2 = pandas.Series(var_list2)
+
     coll = [1, 3, 2]
     cm = CollectionsModel(None, coll)
     assert cm.rowCount() == 3
     assert cm.columnCount() == 5
     cm.sort(0)  # sort by index
-    assert data_table(cm, 3, 4) == [['0', '1', '2'],
+    assert data_table(cm, 3, 4) == [[0, 1, 2],
                                     ['int', 'int', 'int'],
-                                    ['1', '1', '1'],
+                                    [1, 1, 1],
                                     ['1', '3', '2']]
     cm.sort(3)  # sort by value
-    assert data_table(cm, 3, 4) == [['0', '2', '1'],
+    assert data_table(cm, 3, 4) == [[0, 2, 1],
                                     ['int', 'int', 'int'],
-                                    ['1', '1', '1'],
+                                    [1, 1, 1],
                                     ['1', '2', '3']]
-    coll = [[1, 2], 3]
+
+    coll = [1, var_list1, var_list2, var_dataframe1, var_dataframe2,
+            var_series1, var_series2]
     cm = CollectionsModel(None, coll)
-    assert cm.rowCount() == 2
+    assert cm.rowCount() == 7
     assert cm.columnCount() == 5
+
     cm.sort(1)  # sort by type
-    assert data_table(cm, 2, 4) == [['1', '0'],
-                                    ['int', 'list'],
-                                    ['1', '2'],
-                                    ['3', '[1, 2]']]
+    assert data_table(cm, 7, 4) == [
+        [3, 4, 5, 6, 0, 1, 2],
+        ['DataFrame', 'DataFrame', 'Series', 'Series', 'int', 'list', 'list'],
+        ['(3, 3)', '(2, 3)', '(3,)', '(4,)', 1, 3, 4],
+        ['Column names: 0, 1, 2',
+         'Column names: 0, 1, 2',
+         'Series object of pandas.core.series module',
+         'Series object of pandas.core.series module',
+         '1',
+         '[0, 1, 2]',
+         '[3, 4, 5, 6]']]
+
     cm.sort(2)  # sort by size
-    assert data_table(cm, 2, 4) == [['1', '0'],
-                                    ['int', 'list'],
-                                    ['1', '2'],
-                                    ['3', '[1, 2]']]
+    assert data_table(cm, 7, 4) == [
+        [3, 4, 5, 6, 0, 1, 2],
+        ['DataFrame', 'DataFrame', 'Series', 'Series', 'int', 'list', 'list'],
+        ['(2, 3)', '(3,)', '(3, 3)', '(4,)', 1, 3, 4],
+        ['Column names: 0, 1, 2',
+         'Column names: 0, 1, 2',
+         'Series object of pandas.core.series module',
+         'Series object of pandas.core.series module',
+         '1',
+         '[0, 1, 2]',
+         '[3, 4, 5, 6]']] or data_table(cm, 7, 4) == [
+        [0, 1, 2, 4, 5, 3, 6],
+        [u'int', u'list', u'list', u'DataFrame', u'Series', u'DataFrame',
+         u'Series'],
+        [1, 3, 4, u'(2, 3)', u'(3,)', u'(3, 3)', u'(4,)'],
+        ['1',
+         '[0, 1, 2]',
+         '[3, 4, 5, 6]',
+         'Column names: 0, 1, 2',
+         'Series object of pandas.core.series module',
+         'Column names: 0, 1, 2',
+         'Series object of pandas.core.series module',
+         ]]
 
 
 def test_sort_collectionsmodel_with_many_rows():
diff --git a/spyder/plugins/variableexplorer/widgets/tests/test_namespacebrowser.py b/spyder/plugins/variableexplorer/widgets/tests/test_namespacebrowser.py
--- a/spyder/plugins/variableexplorer/widgets/tests/test_namespacebrowser.py
+++ b/spyder/plugins/variableexplorer/widgets/tests/test_namespacebrowser.py
@@ -79,7 +79,7 @@ def test_sort_by_column(qtbot):
     assert model.columnCount() == 5
     assert data_table(model, 2, 4) == [['a_variable', 'b_variable'],
                                        ['int', 'int'],
-                                       ['1', '1'],
+                                       [1, 1],
                                        ['1', '2']]
 
     with qtbot.waitSignal(header.sectionClicked):
@@ -89,7 +89,7 @@ def test_sort_by_column(qtbot):
     # Check sort effect
     assert data_table(model, 2, 4) == [['b_variable', 'a_variable'],
                                        ['int', 'int'],
-                                       ['1', '1'],
+                                       [1, 1],
                                        ['2', '1']]