From e4e57d9ad54185afc3d888e195244f9a446b85bd Mon Sep 17 00:00:00 2001 From: jreback Date: Wed, 16 Oct 2013 14:22:58 -0400 Subject: [PATCH] API: add excel kw to to_clipboard to preserve compat behavior (related GH5070) --- pandas/core/generic.py | 11 +++++++--- pandas/io/clipboard.py | 36 +++++++++++++++++++++++-------- pandas/io/tests/test_clipboard.py | 8 +++++-- 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 8a2ed9926d630..16f4118d5d1df 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -868,14 +868,19 @@ def load(self, path): # TODO remove in 0.14 warnings.warn("load is deprecated, use pd.read_pickle", FutureWarning) return read_pickle(path) - def to_clipboard(self, sep=None, **kwargs): + def to_clipboard(self, excel=None, sep=None, **kwargs): """ Attempt to write text representation of object to the system clipboard This can be pasted into Excel, for example. Parameters ---------- - sep : optional, defaults to comma + excel : boolean, defaults to True + if True, use the provided separator, writing in a csv + format for allowing easy pasting into excel. + if False, write a string representation of the object + to the clipboard + sep : optional, defaults to tab other keywords are passed to to_csv Notes @@ -886,7 +891,7 @@ def to_clipboard(self, sep=None, **kwargs): - OS X: none """ from pandas.io import clipboard - clipboard.to_clipboard(self, sep, **kwargs) + clipboard.to_clipboard(self, excel=excel, sep=sep, **kwargs) #---------------------------------------------------------------------- # Fancy Indexing diff --git a/pandas/io/clipboard.py b/pandas/io/clipboard.py index 401a0689fe000..51142c9f52655 100644 --- a/pandas/io/clipboard.py +++ b/pandas/io/clipboard.py @@ -26,11 +26,22 @@ def read_clipboard(**kwargs): # pragma: no cover return read_table(StringIO(text), **kwargs) -def to_clipboard(obj, sep=None, **kwargs): # pragma: no cover +def to_clipboard(obj, excel=None, sep=None, **kwargs): # pragma: no cover """ Attempt to write text representation of object to the system clipboard The clipboard can be then pasted into Excel for example. + Parameters + ---------- + obj : the object to write to the clipboard + excel : boolean, defaults to True + if True, use the provided separator, writing in a csv + format for allowing easy pasting into excel. + if False, write a string representation of the object + to the clipboard + sep : optional, defaults to tab + other keywords are passed to to_csv + Notes ----- Requirements for your platform @@ -39,12 +50,19 @@ def to_clipboard(obj, sep=None, **kwargs): # pragma: no cover - OS X: """ from pandas.util.clipboard import clipboard_set - try: - if sep is None: - sep = '\t' - buf = StringIO() - obj.to_csv(buf,sep=sep, **kwargs) - clipboard_set(buf.getvalue()) - except: - clipboard_set(str(obj)) + if excel is None: + excel = True + + if excel: + try: + if sep is None: + sep = '\t' + buf = StringIO() + obj.to_csv(buf,sep=sep, **kwargs) + clipboard_set(buf.getvalue()) + return + except: + pass + + clipboard_set(str(obj)) diff --git a/pandas/io/tests/test_clipboard.py b/pandas/io/tests/test_clipboard.py index 90ec2d6fed0ce..45b479ebb589e 100644 --- a/pandas/io/tests/test_clipboard.py +++ b/pandas/io/tests/test_clipboard.py @@ -39,9 +39,9 @@ def setUpClass(cls): def tearDownClass(cls): del cls.data_types, cls.data - def check_round_trip_frame(self, data_type, sep=None): + def check_round_trip_frame(self, data_type, excel=None, sep=None): data = self.data[data_type] - data.to_clipboard(sep=sep) + data.to_clipboard(excel=excel, sep=sep) if sep is not None: result = read_clipboard(sep=sep,index_col=0) else: @@ -52,6 +52,10 @@ def test_round_trip_frame_sep(self): for dt in self.data_types: self.check_round_trip_frame(dt,sep=',') + def test_round_trip_frame_string(self): + for dt in self.data_types: + self.check_round_trip_frame(dt,excel=False) + def test_round_trip_frame(self): for dt in self.data_types: self.check_round_trip_frame(dt)