Repository: spyder-ide/spyder

Issue:
Error when copying text from a Dataframe on Python 2
## Description

**What steps will reproduce the problem?**

1. 
2. 
3. 

**What is the expected output? What do you see instead?**


**Please provide any additional information below**

  File "/Users/ravimalhotra/anaconda/lib/python2.7/site-packages/spyder/widgets/variableexplorer/dataframeeditor.py", line 626, in copy
    obj.to_csv(output, sep='\t', index=index, header=header)
  File "/Users/ravimalhotra/anaconda/lib/python2.7/site-packages/pandas/core/frame.py", line 1413, in to_csv
    formatter.save()
  File "/Users/ravimalhotra/anaconda/lib/python2.7/site-packages/pandas/io/formats/format.py", line 1583, in save
    self._save()
  File "/Users/ravimalhotra/anaconda/lib/python2.7/site-packages/pandas/io/formats/format.py", line 1684, in _save
    self._save_chunk(start_i, end_i)
  File "/Users/ravimalhotra/anaconda/lib/python2.7/site-packages/pandas/io/formats/format.py", line 1710, in _save_chunk
    lib.write_csv_rows(self.data, ix, self.nlevels, self.cols, self.writer)
  File "pandas/_libs/lib.pyx", line 1094, in pandas._libs.lib.write_csv_rows (pandas/_libs/lib.c:19647)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 11: ordinal not in range(128)


## Version and main components

* Spyder Version: 3.2.0 
* Python Version: 2.7.13
* Qt Versions: 5.6.2, PyQt5 5.6 on Darwin

## Dependencies
```
pyflakes >=0.5.0  :  1.5.0 (OK)
pycodestyle >=2.3 :  2.3.1 (OK)
pygments >=2.0    :  2.2.0 (OK)
pandas >=0.13.1   :  0.20.1 (OK)
numpy >=1.7       :  1.12.1 (OK)
sphinx >=0.6.6    :  1.6.3 (OK)
rope >=0.9.4      :  0.10.5 (OK)
jedi >=0.9.0      :  0.10.2 (OK)
psutil >=0.3      :  5.2.2 (OK)
nbconvert >=4.0   :  5.2.1 (OK)
sympy >=0.7.3     :  1.0 (OK)
cython >=0.21     :  0.25.2 (OK)
qtconsole >=4.2.0 :  4.3.0 (OK)
IPython >=4.0;<6.0:  5.3.0 (OK)
pylint >=0.25     :  1.7.2 (OK)

```



Discussion:
Thanks for reporting. I think you could have better luck with non-ascii text in a Dataframe by using Python 3 instead of Python 2.

Since it's not possible to guess what encoding your text is using, we will simply show a dialog saying that text can't be copied.
With a month to go with official Python 2 support I would say this is a won't fix. Closing @ccordoba12 ?
We can catch the UnicodeEncodeError thrown by this kind of error and show the message I mentioned above.
@dalthviz, please take a look at this one.

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
@@ -601,7 +601,15 @@ def copy(self):
         obj = df.iloc[slice(row_min, row_max + 1),
                       slice(col_min, col_max + 1)]
         output = io.StringIO()
-        obj.to_csv(output, sep='\t', index=index, header=header)
+        try:
+            obj.to_csv(output, sep='\t', index=index, header=header)
+        except UnicodeEncodeError:
+            # Needed to handle enconding errors in Python 2
+            # See spyder-ide/spyder#4833
+            QMessageBox.critical(
+                self,
+                _("Error"),
+                _("Text can't be copied."))
         if not PY2:
             contents = output.getvalue()
         else:


Test patch: