Skip to content

API: add excel kw to to_clipboard to preserve compat behavior (related GH5070) #5243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 16, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
36 changes: 27 additions & 9 deletions pandas/io/clipboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))

8 changes: 6 additions & 2 deletions pandas/io/tests/test_clipboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)