Skip to content

Commit 8a2cd85

Browse files
committed
ENH: Pandas Series provide to_excel method
Closes pandas-dev#8825
1 parent 1b0333b commit 8a2cd85

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

doc/source/whatsnew/v0.20.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Other enhancements
4949
^^^^^^^^^^^^^^^^^^
5050

5151
- ``pd.read_excel`` now preserves sheet order when using ``sheetname=None`` (:issue:`9930`)
52-
52+
- ``Series`` provides a ``to_excel`` method to output Excel files (:issue:`8825`)
5353

5454
.. _whatsnew_0200.api_breaking:
5555

pandas/core/series.py

+66
Original file line numberDiff line numberDiff line change
@@ -2619,6 +2619,72 @@ def to_csv(self, path=None, index=True, sep=",", na_rep='',
26192619
if path is None:
26202620
return result
26212621

2622+
def to_excel(self, excel_writer, sheet_name='Sheet1', na_rep='',
2623+
float_format=None, columns=None, header=True, index=True,
2624+
index_label=None, startrow=0, startcol=0, engine=None,
2625+
merge_cells=True, encoding=None, inf_rep='inf', verbose=True):
2626+
"""
2627+
Write Series to a excel sheet
2628+
2629+
Parameters
2630+
----------
2631+
excel_writer : string or ExcelWriter object
2632+
File path or existing ExcelWriter
2633+
sheet_name : string, default 'Sheet1'
2634+
Name of sheet which will contain DataFrame
2635+
na_rep : string, default ''
2636+
Missing data representation
2637+
float_format : string, default None
2638+
Format string for floating point numbers
2639+
columns : sequence, optional
2640+
Columns to write
2641+
header : boolean or list of string, default True
2642+
Write out column names. If a list of string is given it is
2643+
assumed to be aliases for the column names
2644+
index : boolean, default True
2645+
Write row names (index)
2646+
index_label : string or sequence, default None
2647+
Column label for index column(s) if desired. If None is given, and
2648+
`header` and `index` are True, then the index names are used. A
2649+
sequence should be given if the DataFrame uses MultiIndex.
2650+
startrow :
2651+
upper left cell row to dump data frame
2652+
startcol :
2653+
upper left cell column to dump data frame
2654+
engine : string, default None
2655+
write engine to use - you can also set this via the options
2656+
``io.excel.xlsx.writer``, ``io.excel.xls.writer``, and
2657+
``io.excel.xlsm.writer``.
2658+
merge_cells : boolean, default True
2659+
Write MultiIndex and Hierarchical Rows as merged cells.
2660+
encoding: string, default None
2661+
encoding of the resulting excel file. Only necessary for xlwt,
2662+
other writers support unicode natively.
2663+
inf_rep : string, default 'inf'
2664+
Representation for infinity (there is no native representation for
2665+
infinity in Excel)
2666+
2667+
Notes
2668+
-----
2669+
If passing an existing ExcelWriter object, then the sheet will be added
2670+
to the existing workbook. This can be used to save different
2671+
DataFrames to one workbook:
2672+
2673+
>>> writer = ExcelWriter('output.xlsx')
2674+
>>> s.to_excel(writer,'Sheet1')
2675+
>>> s.to_excel(writer,'Sheet2')
2676+
>>> writer.save()
2677+
2678+
For compatibility with to_csv, to_excel serializes lists and dicts to
2679+
strings before writing.
2680+
"""
2681+
from pandas.core.frame import DataFrame
2682+
df = DataFrame(self)
2683+
df.to_excel(excel_writer, sheet_name, na_rep,
2684+
float_format, columns, header, index,
2685+
index_label, startrow, startcol, engine,
2686+
merge_cells, encoding, inf_rep, verbose)
2687+
26222688
def dropna(self, axis=0, inplace=False, **kwargs):
26232689
"""
26242690
Return Series without null values

pandas/io/tests/test_excel.py

+6
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,12 @@ def test_roundtrip(self):
10511051
recons = read_excel(path, index_col=0)
10521052
tm.assert_frame_equal(self.frame, recons)
10531053

1054+
# GH 8825 Pandas Series should provide to_excel method
1055+
s = self.frame["A"]
1056+
s.to_excel(path)
1057+
recons = read_excel(path, index_col=0)
1058+
tm.assert_frame_equal(s.to_frame(), recons)
1059+
10541060
def test_mixed(self):
10551061
_skip_if_no_xlrd()
10561062

0 commit comments

Comments
 (0)