Skip to content

Commit 791d545

Browse files
committed
Merge pull request #5243 from jreback/clip2
API: add excel kw to to_clipboard to preserve compat behavior (related GH5070)
2 parents bb6f3a8 + e4e57d9 commit 791d545

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

pandas/core/generic.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -868,14 +868,19 @@ 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, sep=None, **kwargs):
871+
def to_clipboard(self, excel=None, sep=None, **kwargs):
872872
"""
873873
Attempt to write text representation of object to the system clipboard
874874
This can be pasted into Excel, for example.
875875
876876
Parameters
877877
----------
878-
sep : optional, defaults to comma
878+
excel : boolean, defaults to True
879+
if True, use the provided separator, writing in a csv
880+
format for allowing easy pasting into excel.
881+
if False, write a string representation of the object
882+
to the clipboard
883+
sep : optional, defaults to tab
879884
other keywords are passed to to_csv
880885
881886
Notes
@@ -886,7 +891,7 @@ def to_clipboard(self, sep=None, **kwargs):
886891
- OS X: none
887892
"""
888893
from pandas.io import clipboard
889-
clipboard.to_clipboard(self, sep, **kwargs)
894+
clipboard.to_clipboard(self, excel=excel, sep=sep, **kwargs)
890895

891896
#----------------------------------------------------------------------
892897
# Fancy Indexing

pandas/io/clipboard.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,22 @@ def read_clipboard(**kwargs): # pragma: no cover
2626
return read_table(StringIO(text), **kwargs)
2727

2828

29-
def to_clipboard(obj, sep=None, **kwargs): # pragma: no cover
29+
def to_clipboard(obj, excel=None, sep=None, **kwargs): # pragma: no cover
3030
"""
3131
Attempt to write text representation of object to the system clipboard
3232
The clipboard can be then pasted into Excel for example.
3333
34+
Parameters
35+
----------
36+
obj : the object to write to the clipboard
37+
excel : boolean, defaults to True
38+
if True, use the provided separator, writing in a csv
39+
format for allowing easy pasting into excel.
40+
if False, write a string representation of the object
41+
to the clipboard
42+
sep : optional, defaults to tab
43+
other keywords are passed to to_csv
44+
3445
Notes
3546
-----
3647
Requirements for your platform
@@ -39,12 +50,19 @@ def to_clipboard(obj, sep=None, **kwargs): # pragma: no cover
3950
- OS X:
4051
"""
4152
from pandas.util.clipboard import clipboard_set
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))
53+
if excel is None:
54+
excel = True
55+
56+
if excel:
57+
try:
58+
if sep is None:
59+
sep = '\t'
60+
buf = StringIO()
61+
obj.to_csv(buf,sep=sep, **kwargs)
62+
clipboard_set(buf.getvalue())
63+
return
64+
except:
65+
pass
66+
67+
clipboard_set(str(obj))
5068

pandas/io/tests/test_clipboard.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ def setUpClass(cls):
3939
def tearDownClass(cls):
4040
del cls.data_types, cls.data
4141

42-
def check_round_trip_frame(self, data_type, sep=None):
42+
def check_round_trip_frame(self, data_type, excel=None, sep=None):
4343
data = self.data[data_type]
44-
data.to_clipboard(sep=sep)
44+
data.to_clipboard(excel=excel, sep=sep)
4545
if sep is not None:
4646
result = read_clipboard(sep=sep,index_col=0)
4747
else:
@@ -52,6 +52,10 @@ def test_round_trip_frame_sep(self):
5252
for dt in self.data_types:
5353
self.check_round_trip_frame(dt,sep=',')
5454

55+
def test_round_trip_frame_string(self):
56+
for dt in self.data_types:
57+
self.check_round_trip_frame(dt,excel=False)
58+
5559
def test_round_trip_frame(self):
5660
for dt in self.data_types:
5761
self.check_round_trip_frame(dt)

0 commit comments

Comments
 (0)