Skip to content

Commit df02b00

Browse files
committed
ENH: Pandas Series provide to_excel method
Closes #8825
1 parent 1b0333b commit df02b00

File tree

6 files changed

+80
-56
lines changed

6 files changed

+80
-56
lines changed

doc/source/api.rst

+1
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,7 @@ Serialization / IO / Conversion
692692
Series.to_pickle
693693
Series.to_csv
694694
Series.to_dict
695+
Series.to_excel
695696
Series.to_frame
696697
Series.to_xarray
697698
Series.to_hdf

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/frame.py

+1-55
Original file line numberDiff line numberDiff line change
@@ -1385,65 +1385,11 @@ def to_csv(self, path_or_buf=None, sep=",", na_rep='', float_format=None,
13851385
if path_or_buf is None:
13861386
return formatter.path_or_buf.getvalue()
13871387

1388+
@Appender(_shared_docs['to_excel'] % _shared_doc_kwargs)
13881389
def to_excel(self, excel_writer, sheet_name='Sheet1', na_rep='',
13891390
float_format=None, columns=None, header=True, index=True,
13901391
index_label=None, startrow=0, startcol=0, engine=None,
13911392
merge_cells=True, encoding=None, inf_rep='inf', verbose=True):
1392-
"""
1393-
Write DataFrame to a excel sheet
1394-
1395-
Parameters
1396-
----------
1397-
excel_writer : string or ExcelWriter object
1398-
File path or existing ExcelWriter
1399-
sheet_name : string, default 'Sheet1'
1400-
Name of sheet which will contain DataFrame
1401-
na_rep : string, default ''
1402-
Missing data representation
1403-
float_format : string, default None
1404-
Format string for floating point numbers
1405-
columns : sequence, optional
1406-
Columns to write
1407-
header : boolean or list of string, default True
1408-
Write out column names. If a list of string is given it is
1409-
assumed to be aliases for the column names
1410-
index : boolean, default True
1411-
Write row names (index)
1412-
index_label : string or sequence, default None
1413-
Column label for index column(s) if desired. If None is given, and
1414-
`header` and `index` are True, then the index names are used. A
1415-
sequence should be given if the DataFrame uses MultiIndex.
1416-
startrow :
1417-
upper left cell row to dump data frame
1418-
startcol :
1419-
upper left cell column to dump data frame
1420-
engine : string, default None
1421-
write engine to use - you can also set this via the options
1422-
``io.excel.xlsx.writer``, ``io.excel.xls.writer``, and
1423-
``io.excel.xlsm.writer``.
1424-
merge_cells : boolean, default True
1425-
Write MultiIndex and Hierarchical Rows as merged cells.
1426-
encoding: string, default None
1427-
encoding of the resulting excel file. Only necessary for xlwt,
1428-
other writers support unicode natively.
1429-
inf_rep : string, default 'inf'
1430-
Representation for infinity (there is no native representation for
1431-
infinity in Excel)
1432-
1433-
Notes
1434-
-----
1435-
If passing an existing ExcelWriter object, then the sheet will be added
1436-
to the existing workbook. This can be used to save different
1437-
DataFrames to one workbook:
1438-
1439-
>>> writer = ExcelWriter('output.xlsx')
1440-
>>> df1.to_excel(writer,'Sheet1')
1441-
>>> df2.to_excel(writer,'Sheet2')
1442-
>>> writer.save()
1443-
1444-
For compatibility with to_csv, to_excel serializes lists and dicts to
1445-
strings before writing.
1446-
"""
14471393
from pandas.io.excel import ExcelWriter
14481394
need_save = False
14491395
if encoding is None:

pandas/core/generic.py

+58
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,64 @@ def __setstate__(self, state):
10161016
# ----------------------------------------------------------------------
10171017
# I/O Methods
10181018

1019+
_shared_docs['to_excel'] = """
1020+
Write %(klass)s to a excel sheet
1021+
1022+
.. versionadded:: 0.20.0
1023+
1024+
Parameters
1025+
----------
1026+
excel_writer : string or ExcelWriter object
1027+
File path or existing ExcelWriter
1028+
sheet_name : string, default 'Sheet1'
1029+
Name of sheet which will contain DataFrame
1030+
na_rep : string, default ''
1031+
Missing data representation
1032+
float_format : string, default None
1033+
Format string for floating point numbers
1034+
columns : sequence, optional
1035+
Columns to write
1036+
header : boolean or list of string, default True
1037+
Write out column names. If a list of string is given it is
1038+
assumed to be aliases for the column names
1039+
index : boolean, default True
1040+
Write row names (index)
1041+
index_label : string or sequence, default None
1042+
Column label for index column(s) if desired. If None is given, and
1043+
`header` and `index` are True, then the index names are used. A
1044+
sequence should be given if the DataFrame uses MultiIndex.
1045+
startrow :
1046+
upper left cell row to dump data frame
1047+
startcol :
1048+
upper left cell column to dump data frame
1049+
engine : string, default None
1050+
write engine to use - you can also set this via the options
1051+
``io.excel.xlsx.writer``, ``io.excel.xls.writer``, and
1052+
``io.excel.xlsm.writer``.
1053+
merge_cells : boolean, default True
1054+
Write MultiIndex and Hierarchical Rows as merged cells.
1055+
encoding: string, default None
1056+
encoding of the resulting excel file. Only necessary for xlwt,
1057+
other writers support unicode natively.
1058+
inf_rep : string, default 'inf'
1059+
Representation for infinity (there is no native representation for
1060+
infinity in Excel)
1061+
1062+
Notes
1063+
-----
1064+
If passing an existing ExcelWriter object, then the sheet will be added
1065+
to the existing workbook. This can be used to save different
1066+
DataFrames to one workbook:
1067+
1068+
>>> writer = ExcelWriter('output.xlsx')
1069+
>>> df1.to_excel(writer,'Sheet1')
1070+
>>> df2.to_excel(writer,'Sheet2')
1071+
>>> writer.save()
1072+
1073+
For compatibility with to_csv, to_excel serializes lists and dicts to
1074+
strings before writing.
1075+
"""
1076+
10191077
def to_json(self, path_or_buf=None, orient=None, date_format='epoch',
10201078
double_precision=10, force_ascii=True, date_unit='ms',
10211079
default_handler=None, lines=False):

pandas/core/series.py

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

2622+
@Appender(generic._shared_docs['to_excel'] % _shared_doc_kwargs)
2623+
def to_excel(self, excel_writer, sheet_name='Sheet1', na_rep='',
2624+
float_format=None, columns=None, header=True, index=True,
2625+
index_label=None, startrow=0, startcol=0, engine=None,
2626+
merge_cells=True, encoding=None, inf_rep='inf', verbose=True):
2627+
df = self.to_frame()
2628+
df.to_excel(excel_writer=excel_writer, sheet_name=sheet_name,
2629+
na_rep=na_rep, float_format=float_format, columns=columns,
2630+
header=header, index=index, index_label=index_label,
2631+
startrow=startrow, startcol=startcol, engine=engine,
2632+
merge_cells=merge_cells, encoding=encoding,
2633+
inf_rep=inf_rep, verbose=verbose)
2634+
26222635
def dropna(self, axis=0, inplace=False, **kwargs):
26232636
"""
26242637
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)