Skip to content

Commit eba6001

Browse files
committed
Merge pull request #5070 from jreback/clip
API: default export for to_clipboard is now csv (GH3368)
2 parents f58a438 + 35b71be commit eba6001

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

doc/source/release.rst

+2
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ API Changes
207207
- ``Series.get`` with negative indexers now returns the same as ``[]`` (:issue:`4390`)
208208
- allow ``ix/loc`` for Series/DataFrame/Panel to set on any axis even when the single-key is not currently contained in
209209
the index for that axis (:issue:`2578`, :issue:`5226`)
210+
- Default export for ``to_clipboard`` is now csv with a sep of `\t` for
211+
compat (:issue:`3368`)
210212
- ``at`` now will enlarge the object inplace (and return the same) (:issue:`2578`)
211213

212214
- ``HDFStore``

pandas/core/generic.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -868,9 +868,15 @@ def load(self, path): # TODO remove in 0.14
868868
warnings.warn("load is deprecated, use pd.read_pickle", FutureWarning)
869869
return read_pickle(path)
870870

871-
def to_clipboard(self):
871+
def to_clipboard(self, sep=None, **kwargs):
872872
"""
873873
Attempt to write text representation of object to the system clipboard
874+
This can be pasted into Excel, for example.
875+
876+
Parameters
877+
----------
878+
sep : optional, defaults to comma
879+
other keywords are passed to to_csv
874880
875881
Notes
876882
-----
@@ -880,7 +886,7 @@ def to_clipboard(self):
880886
- OS X: none
881887
"""
882888
from pandas.io import clipboard
883-
clipboard.to_clipboard(self)
889+
clipboard.to_clipboard(self, sep, **kwargs)
884890

885891
#----------------------------------------------------------------------
886892
# Fancy Indexing

pandas/io/clipboard.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ def read_clipboard(**kwargs): # pragma: no cover
2626
return read_table(StringIO(text), **kwargs)
2727

2828

29-
def to_clipboard(obj): # pragma: no cover
29+
def to_clipboard(obj, sep=None, **kwargs): # pragma: no cover
3030
"""
3131
Attempt to write text representation of object to the system clipboard
32+
The clipboard can be then pasted into Excel for example.
3233
3334
Notes
3435
-----
@@ -38,4 +39,12 @@ def to_clipboard(obj): # pragma: no cover
3839
- OS X:
3940
"""
4041
from pandas.util.clipboard import clipboard_set
41-
clipboard_set(str(obj))
42+
try:
43+
if sep is None:
44+
sep = '\t'
45+
buf = StringIO()
46+
obj.to_csv(buf,sep=sep, **kwargs)
47+
clipboard_set(buf.getvalue())
48+
except:
49+
clipboard_set(str(obj))
50+

pandas/io/tests/test_clipboard.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,19 @@ def setUpClass(cls):
3939
def tearDownClass(cls):
4040
del cls.data_types, cls.data
4141

42-
def check_round_trip_frame(self, data_type):
42+
def check_round_trip_frame(self, data_type, sep=None):
4343
data = self.data[data_type]
44-
data.to_clipboard()
45-
result = read_clipboard()
44+
data.to_clipboard(sep=sep)
45+
if sep is not None:
46+
result = read_clipboard(sep=sep,index_col=0)
47+
else:
48+
result = read_clipboard()
4649
tm.assert_frame_equal(data, result, check_dtype=False)
4750

51+
def test_round_trip_frame_sep(self):
52+
for dt in self.data_types:
53+
self.check_round_trip_frame(dt,sep=',')
54+
4855
def test_round_trip_frame(self):
4956
for dt in self.data_types:
5057
self.check_round_trip_frame(dt)

0 commit comments

Comments
 (0)