-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from 25 commits
742aa3b
a8c098d
1fee38f
5204489
fd1d3dd
8439dfe
753e239
ba4bc36
ef8bf54
4d8a1aa
ce02a40
b46159a
f698ed6
2b7b891
009e3e9
c4cc756
cd4be4b
f7bc16f
30f5d78
e363374
1a3a6d2
5013d67
24a650f
5db662f
3939bf3
e50b752
676a58c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
from pandas.compat import StringIO | ||
import warnings | ||
|
||
|
||
def read_clipboard(sep=r'\s+', **kwargs): # pragma: no cover | ||
|
@@ -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, return to default | ||
if sep is None and kwargs.get('delim_whitespace') is None: | ||
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' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that's a good question, we should check what |
||
' 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 consistency, | ||
# encode strings for python engine so that output from python and c | ||
# engines produce consistent results | ||
if kwargs.get('engine') == 'python' and compat.PY2: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
||
|
||
|
@@ -99,17 +116,21 @@ 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) | ||
text = buf.getvalue() | ||
if PY2: | ||
if compat.PY2: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor point but we generally just import PY2 directly (you use it in 2 places) |
||
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 ' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this tested? |
||
'the separator') | ||
elif sep is not None: | ||
warnings.warn('to_clipboard with excel=False ignores the sep argument') | ||
|
||
if isinstance(obj, DataFrame): | ||
# str(df) has various unhelpful defaults, like truncation | ||
|
There was a problem hiding this comment.
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).