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 19 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
27 changes: 23 additions & 4 deletions pandas/io/clipboards.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
""" io on the clipboard """
from pandas import compat, get_option, option_context, DataFrame
from pandas.compat import StringIO, PY2
import warnings


def read_clipboard(sep=r'\s+', **kwargs): # pragma: no cover
Expand Down Expand Up @@ -55,11 +56,27 @@ 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'

# Edge case where sep is specified to be None
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+'

# Regex separator currently only works with python engine.
# Default to python if separator is multi-character (regex)
if len(sep) > 1 and kwargs.get('engine') is None:
kwargs['engine'] = 'python'
elif len(sep) > 1 and kwargs.get('engine') == 'c':
warnings.warn('from_clipboard with regex separator does not work'
Copy link
Member

Choose a reason for hiding this comment

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

What was the reasoning for going with a warning here instead of an error? Curious what actually happens if this comes up (question maybe applicable to other errors as well)

Copy link
Member

Choose a reason for hiding this comment

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

I think that's a good question, we should check what read_csv does (warning or erroring)
But anyhow, if we change it to an error, that would be for 0.24.0 IMO, so another PR.

' properly with c engine')

# In PY2, the c table reader first encodes text with UTF-8 but Python
# table reader uses the format of the passed string.
# For PY2, encode strings first so that output from python and c
# engines produce consistent results
if kwargs.get('engine') == 'python' and compat.PY2:
Copy link
Contributor

Choose a reason for hiding this comment

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

why is the engine important here, shouldn't we alway encode in PY2?

text = text.encode('utf-8')

return read_table(StringIO(text), sep=sep, **kwargs)


Expand Down Expand Up @@ -99,7 +116,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 All @@ -108,8 +125,10 @@ def to_clipboard(obj, excel=True, sep=None, **kwargs): # pragma: no cover
text = text.decode('utf-8')
clipboard_set(text)
return
except:
pass
except TypeError:
warnings.warn('to_clipboard in excel mode requires a single '
'character separator. Set "excel=False" or change '
Copy link
Contributor

Choose a reason for hiding this comment

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

is this tested?

'the separator')

if isinstance(obj, DataFrame):
# str(df) has various unhelpful defaults, like truncation
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/io/test_clipboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ def setup_class(cls):
def teardown_class(cls):
del cls.data_types, cls.data

def test_read_delim_warning(self):
with tm.assert_produces_warning():
self.data['string'].to_clipboard()
pd.read_clipboard(sep=r'\s+', engine='c')

def test_write_delim_warning(self):
with tm.assert_produces_warning():
self.data['string'].to_clipboard(excel=True, sep=r'\s+')

def check_round_trip_frame(self, data_type, excel=None, sep=None,
encoding=None):
data = self.data[data_type]
Expand Down
Empty file modified setup.py
100755 → 100644
Empty file.