Skip to content

BUG: to_clipboard fails to format output for Excel #21111

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
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
742aa3b
Fixed copy table to excel
david-liu-brattle-1 May 17, 2018
a8c098d
Unit Test and whatsnew
david-liu-brattle-1 May 18, 2018
1fee38f
Revert "Unit Test and whatsnew"
david-liu-brattle-1 May 18, 2018
5204489
Merge remote-tracking branch 'upstream/master' into fix-excel-clipboard
david-liu-brattle-1 May 18, 2018
fd1d3dd
Unit test for excel clipboard IO and updated whatsnew
david-liu-brattle-1 May 18, 2018
8439dfe
PEP8
david-liu-brattle-1 May 18, 2018
753e239
Test for function default values
david-liu-brattle-1 May 18, 2018
ba4bc36
More robust clipboard tests
david-liu-brattle-1 May 18, 2018
ef8bf54
Test for correct shape when results aren't expected to exactly match
david-liu-brattle-1 May 18, 2018
4d8a1aa
PEP8
david-liu-brattle-1 May 18, 2018
ce02a40
Formatting
david-liu-brattle-1 May 18, 2018
b46159a
Merge branch 'master' into fix-excel-clipboard
david-liu-brattle-1 Jun 6, 2018
f698ed6
Rebase
david-liu-brattle-1 Jun 6, 2018
2b7b891
Typo
david-liu-brattle-1 Jun 6, 2018
009e3e9
Fixed python 27 compatibility
david-liu-brattle-1 Jun 6, 2018
c4cc756
Merge branch 'master' into fix-excel-clipboard
david-liu-brattle-1 Jun 6, 2018
cd4be4b
Style fix
david-liu-brattle-1 Jun 6, 2018
f7bc16f
Merge branch 'master' of https://github.com/pandas-dev/pandas into fi…
david-liu-brattle-1 Jun 18, 2018
30f5d78
Fix test
david-liu-brattle-1 Jun 18, 2018
e363374
Added warning for excel=False and sep!=None
david-liu-brattle-1 Jun 23, 2018
1a3a6d2
Merge branch 'master' into fix-excel-clipboard
david-liu-brattle-1 Jun 27, 2018
5013d67
Removed xfail, add whatsnew
david-liu-brattle-1 Jun 27, 2018
24a650f
Rebuild
david-liu-brattle-1 Jun 27, 2018
5db662f
Typo fixes
david-liu-brattle-1 Jun 27, 2018
3939bf3
permissions
david-liu-brattle-1 Jun 27, 2018
e50b752
Merge remote-tracking branch 'upstream/master' into david-liu-brattle…
jorisvandenbossche Jun 29, 2018
676a58c
fix warning + small edits
jorisvandenbossche Jun 29, 2018
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: 1 addition & 1 deletion doc/source/whatsnew/v0.23.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Indexing
I/O
^^^

-
-Bug in :func:`DataFrame.to_clipboard` where data sent to clipboard was not properly tab-delimited even when ``excel=True`` (:issue:`21104`)
-

Plotting
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/clipboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def read_clipboard(sep=r'\s+', **kwargs): # pragma: no cover

counts = {x.lstrip().count('\t') for x in lines}
if len(lines) > 1 and len(counts) == 1 and counts.pop() != 0:
sep = r'\t'
sep = '\t'

if sep is None and kwargs.get('delim_whitespace') is None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add some comments on these cases (including the existing ones).

sep = r'\s+'
Expand Down Expand Up @@ -99,7 +99,7 @@ def to_clipboard(obj, excel=True, sep=None, **kwargs): # pragma: no cover
if excel:
try:
if sep is None:
sep = r'\t'
sep = '\t'
buf = StringIO()
# clipboard_set (pyperclip) expects unicode
obj.to_csv(buf, sep=sep, encoding='utf-8', **kwargs)
Expand Down
12 changes: 11 additions & 1 deletion pandas/tests/io/test_clipboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from pandas.util import testing as tm
from pandas.util.testing import makeCustomDataframe as mkdf
from pandas.io.clipboard.exceptions import PyperclipException
from pandas.io.clipboard import clipboard_set
from pandas.io.clipboard import clipboard_set, clipboard_get


try:
Expand Down Expand Up @@ -81,6 +81,7 @@ def test_round_trip_frame_sep(self):
self.check_round_trip_frame(dt, sep=',')
self.check_round_trip_frame(dt, sep=r'\s+')
self.check_round_trip_frame(dt, sep='|')
self.check_round_trip_frame(dt, sep='\t')

def test_round_trip_frame_string(self):
for dt in self.data_types:
Expand Down Expand Up @@ -123,6 +124,15 @@ def test_read_clipboard_infer_excel(self):
exp = pd.read_clipboard(**clip_kwargs)

tm.assert_frame_equal(res, exp)

def test_excel_clipboard_format(self):
for dt in self.data_types:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here - can we replace the loops with parametrization?

for sep in ['\t', None]:
data = self.data[dt]
data.to_clipboard(excel=True, sep=sep)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also test with excel=False? In <=0.22 it would also be tab delimited

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean to test data.to_clipboard(excel=False, sep=sep)? I don't think that was (or should be) tab delimited. Do you mean excel=None?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, right, I meant to the default df.to_clipboard() (sep=None, excel=None)

# pandas 0.22
In [72]: df = pd.DataFrame({'a': [1, 3], 'b': [4, 5]})

In [73]: df.to_clipboard()

# <paste>
In [74]: res = """	a	b
    ...: 0	1	4
    ...: 1	3	5"""

In [75]: res.count('\t')
Out[75]: 6

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Should be added

result = read_clipboard(sep='\t', index_col=0)
tm.assert_frame_equal(data, result, check_dtype=False)
assert clipboard_get().count('\t') > 0

def test_invalid_encoding(self):
# test case for testing invalid encoding
Expand Down