From deee809d1e7998f6c4bdf5642fb22eaf8d45e149 Mon Sep 17 00:00:00 2001 From: David Linke Date: Thu, 10 Mar 2016 00:46:04 +0100 Subject: [PATCH 1/2] Fixes #12529 - Improvement of to_clipboard for objects containing unicode. --- doc/source/whatsnew/v0.18.0.txt | 1 + pandas/io/clipboard.py | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/doc/source/whatsnew/v0.18.0.txt b/doc/source/whatsnew/v0.18.0.txt index bdc20d964a06a..fe6035be82e10 100644 --- a/doc/source/whatsnew/v0.18.0.txt +++ b/doc/source/whatsnew/v0.18.0.txt @@ -1128,6 +1128,7 @@ Performance Improvements Bug Fixes ~~~~~~~~~ +- Bug in ``to_clipboard`` when called for objects containing unicode without passing an encoding (:issue:`12529`) - Bug in ``GroupBy.size`` when data-frame is empty. (:issue:`11699`) - Bug in ``Period.end_time`` when a multiple of time period is requested (:issue:`11738`) - Regression in ``.clip`` with tz-aware datetimes (:issue:`11838`) 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 From da252181fdb0e912299c50cbccc8dab33e6c7513 Mon Sep 17 00:00:00 2001 From: David Linke Date: Thu, 10 Mar 2016 01:03:25 +0100 Subject: [PATCH 2/2] Moved note from whatsnew 0.18.0 to 0.18.1 --- doc/source/whatsnew/v0.18.0.txt | 1 - doc/source/whatsnew/v0.18.1.txt | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.18.0.txt b/doc/source/whatsnew/v0.18.0.txt index fe6035be82e10..bdc20d964a06a 100644 --- a/doc/source/whatsnew/v0.18.0.txt +++ b/doc/source/whatsnew/v0.18.0.txt @@ -1128,7 +1128,6 @@ Performance Improvements Bug Fixes ~~~~~~~~~ -- Bug in ``to_clipboard`` when called for objects containing unicode without passing an encoding (:issue:`12529`) - Bug in ``GroupBy.size`` when data-frame is empty. (:issue:`11699`) - Bug in ``Period.end_time`` when a multiple of time period is requested (:issue:`11738`) - Regression in ``.clip`` with tz-aware datetimes (:issue:`11838`) 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`)