From 35b71be96ffb7663c74dab7a7d73bf6e5eac8ce3 Mon Sep 17 00:00:00 2001 From: jreback Date: Tue, 1 Oct 2013 12:39:16 -0400 Subject: [PATCH] API: default export for to_clipboard is now csv (GH3368) --- doc/source/release.rst | 2 ++ pandas/core/generic.py | 10 ++++++++-- pandas/io/clipboard.py | 13 +++++++++++-- pandas/io/tests/test_clipboard.py | 13 ++++++++++--- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/doc/source/release.rst b/doc/source/release.rst index 6ea4e5a3046b2..34cc4e499a0d5 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -207,6 +207,8 @@ API Changes - ``Series.get`` with negative indexers now returns the same as ``[]`` (:issue:`4390`) - allow ``ix/loc`` for Series/DataFrame/Panel to set on any axis even when the single-key is not currently contained in the index for that axis (:issue:`2578`, :issue:`5226`) + - Default export for ``to_clipboard`` is now csv with a sep of `\t` for + compat (:issue:`3368`) - ``at`` now will enlarge the object inplace (and return the same) (:issue:`2578`) - ``HDFStore`` diff --git a/pandas/core/generic.py b/pandas/core/generic.py index bc5d84b9ff0f5..8a2ed9926d630 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -868,9 +868,15 @@ 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): + def to_clipboard(self, 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 + other keywords are passed to to_csv Notes ----- @@ -880,7 +886,7 @@ def to_clipboard(self): - OS X: none """ from pandas.io import clipboard - clipboard.to_clipboard(self) + clipboard.to_clipboard(self, sep, **kwargs) #---------------------------------------------------------------------- # Fancy Indexing diff --git a/pandas/io/clipboard.py b/pandas/io/clipboard.py index c4bea55ce2714..401a0689fe000 100644 --- a/pandas/io/clipboard.py +++ b/pandas/io/clipboard.py @@ -26,9 +26,10 @@ def read_clipboard(**kwargs): # pragma: no cover return read_table(StringIO(text), **kwargs) -def to_clipboard(obj): # pragma: no cover +def to_clipboard(obj, 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. Notes ----- @@ -38,4 +39,12 @@ def to_clipboard(obj): # pragma: no cover - OS X: """ from pandas.util.clipboard import clipboard_set - clipboard_set(str(obj)) + 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)) + diff --git a/pandas/io/tests/test_clipboard.py b/pandas/io/tests/test_clipboard.py index f5b5ba745d83c..90ec2d6fed0ce 100644 --- a/pandas/io/tests/test_clipboard.py +++ b/pandas/io/tests/test_clipboard.py @@ -39,12 +39,19 @@ def setUpClass(cls): def tearDownClass(cls): del cls.data_types, cls.data - def check_round_trip_frame(self, data_type): + def check_round_trip_frame(self, data_type, sep=None): data = self.data[data_type] - data.to_clipboard() - result = read_clipboard() + data.to_clipboard(sep=sep) + if sep is not None: + result = read_clipboard(sep=sep,index_col=0) + else: + result = read_clipboard() tm.assert_frame_equal(data, result, check_dtype=False) + 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(self): for dt in self.data_types: self.check_round_trip_frame(dt)