Repository: pandas-dev/pandas

Issue:
Excel and co. can parse a table from the clipboard, but pandas to_clip uses wrong format



Discussion:
what do you mean by wrong format?

it just writes text, does it need to have a sep for example?

It's been a while, iirc, just open up excel, copy a table and then paste it into an editor,
that's the format. pandas exports something else which excel can't parse.

Should be an easy fix.

Just remembered something else from way back, there's a way to get excel
to accept the data directly without going through the import wizard, I think it needs
to be UTF16LE+BOM. Probably requires some playing around to get it working.

Not necessary, but convenient (for excel anyway).

Just needs to be tab delimited, instead of space delimited. (I was able to
do copy/paste directly with the current setup, it just puts it all in one
column for now):

```
col1\tcol2\tcol3\n
a\tb\tc\n
```

Really simple. I think current versions of Excel (i.e., 2007+) don't need
the header at all.

Should test with libreoffice as well. and unicode.


Resolving patch:
diff --git a/pandas/core/generic.py b/pandas/core/generic.py
--- 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
--- 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))
+


Test patch:
diff --git a/pandas/io/tests/test_clipboard.py b/pandas/io/tests/test_clipboard.py
--- 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)