-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Make os.linesep default line_terminator in to_clipboard #13125
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 all commits
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,5 +1,5 @@ | ||
""" io on the clipboard """ | ||
from pandas import compat, get_option, option_context, DataFrame | ||
from pandas import compat, get_option, option_context, DataFrame, Series | ||
from pandas.compat import StringIO | ||
|
||
|
||
|
@@ -51,7 +51,7 @@ def read_clipboard(**kwargs): # pragma: no cover | |
return read_table(StringIO(text), **kwargs) | ||
|
||
|
||
def to_clipboard(obj, excel=None, sep=None, **kwargs): # pragma: no cover | ||
def to_clipboard(obj, excel=True, **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. | ||
|
@@ -60,12 +60,11 @@ def to_clipboard(obj, excel=None, sep=None, **kwargs): # pragma: no cover | |
---------- | ||
obj : the object to write to the clipboard | ||
excel : boolean, defaults to True | ||
if True, use the provided separator, writing in a csv | ||
format for allowing easy pasting into excel. | ||
if True, use '\\t' separator and os.linesep line terminator by | ||
default, write to csv format to allow easy pasting into excel. | ||
if False, write a string representation of the object | ||
to the clipboard | ||
sep : optional, defaults to tab | ||
other keywords are passed to to_csv | ||
other keywords are passed to to_csv or DataFrame.to_string | ||
|
||
Notes | ||
----- | ||
|
@@ -75,21 +74,22 @@ def to_clipboard(obj, excel=None, sep=None, **kwargs): # pragma: no cover | |
- OS X: | ||
""" | ||
from pandas.util.clipboard import clipboard_set | ||
if excel is None: | ||
excel = True | ||
|
||
if excel: | ||
try: | ||
if sep is None: | ||
sep = '\t' | ||
buf = StringIO() | ||
obj.to_csv(buf, sep=sep, **kwargs) | ||
clipboard_set(buf.getvalue()) | ||
return | ||
except: | ||
pass | ||
|
||
if isinstance(obj, DataFrame): | ||
# COMPAT: allow None for `excel` and `sep` | ||
if excel in (True, None) and isinstance(obj, (DataFrame, Series)): | ||
if 'sep' not in kwargs or not kwargs['sep']: | ||
kwargs['sep'] = '\t' | ||
if 'line_terminator' not in kwargs: | ||
import os | ||
kwargs['line_terminator'] = os.linesep | ||
buf = StringIO() | ||
obj.to_csv(buf, **kwargs) | ||
objstr = buf.getvalue() | ||
elif isinstance(obj, DataFrame): | ||
if 'sep' in kwargs: | ||
if kwargs['sep']: | ||
raise ValueError("sep kw is unsupported with excel=False") | ||
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. maybe i should remove |
||
del kwargs['sep'] # sep is \t | ||
# str(df) has various unhelpful defaults, like truncation | ||
with option_context('display.max_colwidth', 999999): | ||
objstr = obj.to_string(**kwargs) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
# -*- coding: utf-8 -*- | ||
import os | ||
|
||
import numpy as np | ||
from numpy.random import randint | ||
|
||
|
@@ -80,6 +82,14 @@ def test_round_trip_frame(self): | |
for dt in self.data_types: | ||
self.check_round_trip_frame(dt) | ||
|
||
def test_linesep(self): | ||
pd.DataFrame().to_clipboard() | ||
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. need a fair amount more tests. these need to exercise all of the paths that are defined above. 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. You mean check all |
||
ret = pandas.util.clipboard.clipboard_get() | ||
assert ret == '""'+os.linesep, "Wrong line separator" | ||
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. need to use assert_frame/series_equal. 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. Well, i check that text data in the clipboard has intended separators (os.linesep). How can this be done with |
||
pd.Series([0]).to_clipboard(line_terminator="\t") | ||
ret = pandas.util.clipboard.clipboard_get() | ||
assert ret == "0\t0\t", "Wrong line separator" | ||
|
||
def test_read_clipboard_infer_excel(self): | ||
from textwrap import dedent | ||
from pandas.util.clipboard import clipboard_set | ||
|
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.
this is looking way more complicated than before.
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.
I tried to make this compatible with current
to_clipboard
behaviour and its tests.excel=None
it's treated asTrue
. Existing tests useNone
.sep=None
is ignored'Cause i thought it's better to pass
sep
as akwargs
item i have to check for its existense&value. Is it better to convert it back to explicit function argument?