Skip to content

Commit

Permalink
ENH: Pandas Series provide to_excel method
Browse files Browse the repository at this point in the history
  • Loading branch information
scls19fr committed Dec 1, 2016
1 parent 1b0333b commit 8a2cd85
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Other enhancements
^^^^^^^^^^^^^^^^^^

- ``pd.read_excel`` now preserves sheet order when using ``sheetname=None`` (:issue:`9930`)

- ``Series`` provides a ``to_excel`` method to output Excel files (:issue:`8825`)

.. _whatsnew_0200.api_breaking:

Expand Down
66 changes: 66 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2619,6 +2619,72 @@ def to_csv(self, path=None, index=True, sep=",", na_rep='',
if path is None:
return result

def to_excel(self, excel_writer, sheet_name='Sheet1', na_rep='',
float_format=None, columns=None, header=True, index=True,
index_label=None, startrow=0, startcol=0, engine=None,
merge_cells=True, encoding=None, inf_rep='inf', verbose=True):
"""
Write Series to a excel sheet
Parameters
----------
excel_writer : string or ExcelWriter object
File path or existing ExcelWriter
sheet_name : string, default 'Sheet1'
Name of sheet which will contain DataFrame
na_rep : string, default ''
Missing data representation
float_format : string, default None
Format string for floating point numbers
columns : sequence, optional
Columns to write
header : boolean or list of string, default True
Write out column names. If a list of string is given it is
assumed to be aliases for the column names
index : boolean, default True
Write row names (index)
index_label : string or sequence, default None
Column label for index column(s) if desired. If None is given, and
`header` and `index` are True, then the index names are used. A
sequence should be given if the DataFrame uses MultiIndex.
startrow :
upper left cell row to dump data frame
startcol :
upper left cell column to dump data frame
engine : string, default None
write engine to use - you can also set this via the options
``io.excel.xlsx.writer``, ``io.excel.xls.writer``, and
``io.excel.xlsm.writer``.
merge_cells : boolean, default True
Write MultiIndex and Hierarchical Rows as merged cells.
encoding: string, default None
encoding of the resulting excel file. Only necessary for xlwt,
other writers support unicode natively.
inf_rep : string, default 'inf'
Representation for infinity (there is no native representation for
infinity in Excel)
Notes
-----
If passing an existing ExcelWriter object, then the sheet will be added
to the existing workbook. This can be used to save different
DataFrames to one workbook:
>>> writer = ExcelWriter('output.xlsx')
>>> s.to_excel(writer,'Sheet1')
>>> s.to_excel(writer,'Sheet2')
>>> writer.save()
For compatibility with to_csv, to_excel serializes lists and dicts to
strings before writing.
"""
from pandas.core.frame import DataFrame
df = DataFrame(self)
df.to_excel(excel_writer, sheet_name, na_rep,
float_format, columns, header, index,
index_label, startrow, startcol, engine,
merge_cells, encoding, inf_rep, verbose)

def dropna(self, axis=0, inplace=False, **kwargs):
"""
Return Series without null values
Expand Down
6 changes: 6 additions & 0 deletions pandas/io/tests/test_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,12 @@ def test_roundtrip(self):
recons = read_excel(path, index_col=0)
tm.assert_frame_equal(self.frame, recons)

# GH 8825 Pandas Series should provide to_excel method
s = self.frame["A"]
s.to_excel(path)
recons = read_excel(path, index_col=0)
tm.assert_frame_equal(s.to_frame(), recons)

def test_mixed(self):
_skip_if_no_xlrd()

Expand Down

0 comments on commit 8a2cd85

Please sign in to comment.