Skip to content

API: default export for to_clipboard is now csv/tsv suitable for excel (GH3368) #5070

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
2 changes: 2 additions & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``
Expand Down
10 changes: 8 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----
Expand All @@ -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
Expand Down
13 changes: 11 additions & 2 deletions pandas/io/clipboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----
Expand All @@ -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))

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