Skip to content

Commit

Permalink
Add to_clipboard for Series
Browse files Browse the repository at this point in the history
  • Loading branch information
garawalid committed May 11, 2019
1 parent 743ec24 commit 53eb41e
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
65 changes: 65 additions & 0 deletions databricks/koalas/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,71 @@ def to_string(self, buf=None, na_rep='NaN', float_format=None, header=True,
return validate_arguments_and_invoke_function(
kseries.to_pandas(), self.to_string, pd.Series.to_string, args)

def to_clipboard(self, excel=True, sep=None, **kwargs):
"""
Copy object to the system clipboard.
Write a text representation of object to the system clipboard.
This can be pasted into Excel, for example.
.. note:: This method should only be used if the resulting DataFrame is expected
to be small, as all the data is loaded into the driver's memory.
Parameters
----------
excel : bool, default True
- True, use the provided separator, writing in a csv format for
allowing easy pasting into excel.
- False, write a string representation of the object to the
clipboard.
sep : str, default ``'\\t'``
Field delimiter.
**kwargs
These parameters will be passed to DataFrame.to_csv.
Notes
-----
Requirements for your platform.
- Linux : `xclip`, or `xsel` (with `gtk` or `PyQt4` modules)
- Windows : none
- OS X : none
Examples
--------
Copy the contents of a DataFrame to the clipboard.
>>> df = ks.Series([1, 2, 3, 4, 5, 6, 7], name='x')
>>> df.to_clipboard(sep=',')
... # Wrote the following to the system clipboard:
... # 0, 1
... # 1, 2
... # 2, 3
... # 3, 4
... # 4, 5
... # 5, 6
... # 6, 7
We can omit the the index by passing the keyword `index` and setting
it to false.
>>> df.to_clipboard(sep=',', index=False)
... # Wrote the following to the system clipboard:
... # 1
... # 2
... # 3
... # 4
... # 5
... # 6
... # 7
"""
args = locals()
kseries = self

return validate_arguments_and_invoke_function(
kseries.to_pandas(), self.to_clipboard, pd.Series.to_clipboard, args)

def to_dict(self, into=dict):
"""
Convert Series to {label -> value} dict or dict-like object.
Expand Down
42 changes: 42 additions & 0 deletions databricks/koalas/tests/test_series_conversion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#
# Copyright (C) 2019 Databricks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


import pandas as pd

from databricks import koalas
from databricks.koalas.testing.utils import ReusedSQLTestCase, SQLTestUtils


class SeriesConversionTest(ReusedSQLTestCase, SQLTestUtils):

@property
def ps(self):
return pd.Series([1, 2, 3, 4, 5, 6, 7], name='x')

@property
def ks(self):
return koalas.from_pandas(self.ps)

def test_to_clipboard(self):
ps = self.ps
ks = self.ks

self.assert_eq(ks.to_clipboard(), ps.to_clipboard())
self.assert_eq(ks.to_clipboard(excel=False),
ps.to_clipboard(excel=False))
self.assert_eq(ks.to_clipboard(sep=',', index=False),
ps.to_clipboard(sep=',', index=False))

0 comments on commit 53eb41e

Please sign in to comment.