Skip to content

Commit 643267c

Browse files
committed
Use to_string() to format DataFrames for clipboard.
Using str() has some unhelpful properties, especially when DataFrames are large enough that the default repr does not show them in their entirety. The defaults for to_string() are more helpful for the clipboard. Closes pandas-devgh-5346
1 parent 5933782 commit 643267c

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

pandas/io/clipboard.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
""" io on the clipboard """
2-
from pandas import compat, get_option
2+
from pandas import compat, get_option, DataFrame
33
from pandas.compat import StringIO
44

55
def read_clipboard(**kwargs): # pragma: no cover
@@ -64,5 +64,10 @@ def to_clipboard(obj, excel=None, sep=None, **kwargs): # pragma: no cover
6464
except:
6565
pass
6666

67-
clipboard_set(str(obj))
67+
if isinstance(obj, DataFrame):
68+
# str(df) has various unhelpful defaults, like truncation
69+
objstr = obj.to_string()
70+
else:
71+
objstr = str(obj)
72+
clipboard_set(objstr)
6873

pandas/io/tests/test_clipboard.py

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from pandas import DataFrame
99
from pandas import read_clipboard
10+
from pandas import get_option
1011
from pandas.util import testing as tm
1112
from pandas.util.testing import makeCustomDataframe as mkdf
1213

@@ -33,6 +34,11 @@ def setUpClass(cls):
3334
cls.data['mixed'] = DataFrame({'a': np.arange(1.0, 6.0) + 0.01,
3435
'b': np.arange(1, 6),
3536
'c': list('abcde')})
37+
# Test GH-5346
38+
max_rows = get_option('display.max_rows')
39+
cls.data['longdf'] = mkdf(max_rows+1, 3, data_gen_f=lambda *args: randint(2),
40+
c_idx_type='s', r_idx_type='i',
41+
c_idx_names=[None], r_idx_names=[None])
3642
cls.data_types = list(cls.data.keys())
3743

3844
@classmethod

0 commit comments

Comments
 (0)