diff --git a/doc/source/whatsnew/v0.18.1.txt b/doc/source/whatsnew/v0.18.1.txt index 70a1ad4a335ea..417988c2f935f 100644 --- a/doc/source/whatsnew/v0.18.1.txt +++ b/doc/source/whatsnew/v0.18.1.txt @@ -43,3 +43,5 @@ Performance Improvements Bug Fixes ~~~~~~~~~ + +- Bug in ``to_clipboard`` when called for objects containing unicode without passing an encoding (:issue:`12529`) diff --git a/pandas/io/clipboard.py b/pandas/io/clipboard.py index 2109e1c5d6d4c..956f3859a04fe 100644 --- a/pandas/io/clipboard.py +++ b/pandas/io/clipboard.py @@ -73,6 +73,9 @@ def to_clipboard(obj, excel=None, sep=None, **kwargs): # pragma: no cover - Linux: xclip, or xsel (with gtk or PyQt4 modules) - Windows: - OS X: + + If the object contains unicode and no encoding is passed as keyword + argument, the default locale will be used. """ from pandas.util.clipboard import clipboard_set if excel is None: @@ -86,6 +89,12 @@ def to_clipboard(obj, excel=None, sep=None, **kwargs): # pragma: no cover obj.to_csv(buf, sep=sep, **kwargs) clipboard_set(buf.getvalue()) return + except UnicodeEncodeError: + # try again with encoding from locale + from locale import getdefaultlocale + obj.to_csv(buf, sep=sep, encoding=getdefaultlocale()[1], **kwargs) + clipboard_set(buf.getvalue()) + return except: pass