diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 2f78c9acf7972..78fd446c4d5d6 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -1211,7 +1211,7 @@ def to_pickle(self, path): from pandas.io.pickle import to_pickle return to_pickle(self, path) - def to_clipboard(self, excel=None, sep=None, **kwargs): + def to_clipboard(self, excel=True, **kwargs): """ Attempt to write text representation of object to the system clipboard This can be pasted into Excel, for example. @@ -1219,12 +1219,11 @@ def to_clipboard(self, excel=None, sep=None, **kwargs): Parameters ---------- 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 ----- @@ -1234,7 +1233,7 @@ def to_clipboard(self, excel=None, sep=None, **kwargs): - OS X: none """ from pandas.io import clipboard - clipboard.to_clipboard(self, excel=excel, sep=sep, **kwargs) + clipboard.to_clipboard(self, excel=excel, **kwargs) def to_xarray(self): """ diff --git a/pandas/core/series.py b/pandas/core/series.py index 8379c8bcdcae8..b2a07b58f2c13 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -2552,7 +2552,8 @@ def from_csv(cls, path, sep=',', parse_dates=True, header=None, def to_csv(self, path=None, index=True, sep=",", na_rep='', float_format=None, header=False, index_label=None, - mode='w', encoding=None, date_format=None, decimal='.'): + mode='w', encoding=None, date_format=None, decimal='.', + line_terminator='\n'): """ Write Series to a comma-separated values (csv) file @@ -2584,6 +2585,8 @@ def to_csv(self, path=None, index=True, sep=",", na_rep='', decimal: string, default '.' Character recognized as decimal separator. E.g. use ',' for European data + line_terminator : string, default '\\n' + The newline character or character sequence to use in the output file """ from pandas.core.frame import DataFrame df = DataFrame(self) @@ -2592,7 +2595,7 @@ def to_csv(self, path=None, index=True, sep=",", na_rep='', float_format=float_format, header=header, index_label=index_label, mode=mode, encoding=encoding, date_format=date_format, - decimal=decimal) + decimal=decimal, line_terminator=line_terminator) if path is None: return result diff --git a/pandas/io/clipboard.py b/pandas/io/clipboard.py index 2109e1c5d6d4c..b4d25108fa58e 100644 --- a/pandas/io/clipboard.py +++ b/pandas/io/clipboard.py @@ -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") + 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) diff --git a/pandas/io/tests/test_clipboard.py b/pandas/io/tests/test_clipboard.py index a7da27a2f75dd..702278dd71dba 100644 --- a/pandas/io/tests/test_clipboard.py +++ b/pandas/io/tests/test_clipboard.py @@ -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() + ret = pandas.util.clipboard.clipboard_get() + assert ret == '""'+os.linesep, "Wrong line separator" + 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