Skip to content

Commit e8c3405

Browse files
CLN: Replace Appender by doc in to_excel (#32787)
1 parent 4f9bc8a commit e8c3405

File tree

2 files changed

+112
-125
lines changed

2 files changed

+112
-125
lines changed

pandas/core/generic.py

+109-111
Original file line numberDiff line numberDiff line change
@@ -1912,117 +1912,7 @@ def _repr_data_resource_(self):
19121912
%(klass)s in Markdown-friendly format.
19131913
"""
19141914

1915-
_shared_docs[
1916-
"to_excel"
1917-
] = """
1918-
Write %(klass)s to an Excel sheet.
1919-
1920-
To write a single %(klass)s to an Excel .xlsx file it is only necessary to
1921-
specify a target file name. To write to multiple sheets it is necessary to
1922-
create an `ExcelWriter` object with a target file name, and specify a sheet
1923-
in the file to write to.
1924-
1925-
Multiple sheets may be written to by specifying unique `sheet_name`.
1926-
With all data written to the file it is necessary to save the changes.
1927-
Note that creating an `ExcelWriter` object with a file name that already
1928-
exists will result in the contents of the existing file being erased.
1929-
1930-
Parameters
1931-
----------
1932-
excel_writer : str or ExcelWriter object
1933-
File path or existing ExcelWriter.
1934-
sheet_name : str, default 'Sheet1'
1935-
Name of sheet which will contain DataFrame.
1936-
na_rep : str, default ''
1937-
Missing data representation.
1938-
float_format : str, optional
1939-
Format string for floating point numbers. For example
1940-
``float_format="%%.2f"`` will format 0.1234 to 0.12.
1941-
columns : sequence or list of str, optional
1942-
Columns to write.
1943-
header : bool or list of str, default True
1944-
Write out the column names. If a list of string is given it is
1945-
assumed to be aliases for the column names.
1946-
index : bool, default True
1947-
Write row names (index).
1948-
index_label : str or sequence, optional
1949-
Column label for index column(s) if desired. If not specified, and
1950-
`header` and `index` are True, then the index names are used. A
1951-
sequence should be given if the DataFrame uses MultiIndex.
1952-
startrow : int, default 0
1953-
Upper left cell row to dump data frame.
1954-
startcol : int, default 0
1955-
Upper left cell column to dump data frame.
1956-
engine : str, optional
1957-
Write engine to use, 'openpyxl' or 'xlsxwriter'. You can also set this
1958-
via the options ``io.excel.xlsx.writer``, ``io.excel.xls.writer``, and
1959-
``io.excel.xlsm.writer``.
1960-
merge_cells : bool, default True
1961-
Write MultiIndex and Hierarchical Rows as merged cells.
1962-
encoding : str, optional
1963-
Encoding of the resulting excel file. Only necessary for xlwt,
1964-
other writers support unicode natively.
1965-
inf_rep : str, default 'inf'
1966-
Representation for infinity (there is no native representation for
1967-
infinity in Excel).
1968-
verbose : bool, default True
1969-
Display more information in the error logs.
1970-
freeze_panes : tuple of int (length 2), optional
1971-
Specifies the one-based bottommost row and rightmost column that
1972-
is to be frozen.
1973-
1974-
See Also
1975-
--------
1976-
to_csv : Write DataFrame to a comma-separated values (csv) file.
1977-
ExcelWriter : Class for writing DataFrame objects into excel sheets.
1978-
read_excel : Read an Excel file into a pandas DataFrame.
1979-
read_csv : Read a comma-separated values (csv) file into DataFrame.
1980-
1981-
Notes
1982-
-----
1983-
For compatibility with :meth:`~DataFrame.to_csv`,
1984-
to_excel serializes lists and dicts to strings before writing.
1985-
1986-
Once a workbook has been saved it is not possible write further data
1987-
without rewriting the whole workbook.
1988-
1989-
Examples
1990-
--------
1991-
1992-
Create, write to and save a workbook:
1993-
1994-
>>> df1 = pd.DataFrame([['a', 'b'], ['c', 'd']],
1995-
... index=['row 1', 'row 2'],
1996-
... columns=['col 1', 'col 2'])
1997-
>>> df1.to_excel("output.xlsx") # doctest: +SKIP
1998-
1999-
To specify the sheet name:
2000-
2001-
>>> df1.to_excel("output.xlsx",
2002-
... sheet_name='Sheet_name_1') # doctest: +SKIP
2003-
2004-
If you wish to write to more than one sheet in the workbook, it is
2005-
necessary to specify an ExcelWriter object:
2006-
2007-
>>> df2 = df1.copy()
2008-
>>> with pd.ExcelWriter('output.xlsx') as writer: # doctest: +SKIP
2009-
... df1.to_excel(writer, sheet_name='Sheet_name_1')
2010-
... df2.to_excel(writer, sheet_name='Sheet_name_2')
2011-
2012-
ExcelWriter can also be used to append to an existing Excel file:
2013-
2014-
>>> with pd.ExcelWriter('output.xlsx',
2015-
... mode='a') as writer: # doctest: +SKIP
2016-
... df.to_excel(writer, sheet_name='Sheet_name_3')
2017-
2018-
To set the library that is used to write the Excel file,
2019-
you can pass the `engine` keyword (the default engine is
2020-
automatically chosen depending on the file extension):
2021-
2022-
>>> df1.to_excel('output1.xlsx', engine='xlsxwriter') # doctest: +SKIP
2023-
"""
2024-
2025-
@Appender(_shared_docs["to_excel"] % dict(klass="object"))
1915+
@doc(klass="object")
20261916
def to_excel(
20271917
self,
20281918
excel_writer,
@@ -2042,6 +1932,114 @@ def to_excel(
20421932
verbose=True,
20431933
freeze_panes=None,
20441934
) -> None:
1935+
"""
1936+
Write {klass} to an Excel sheet.
1937+
1938+
To write a single {klass} to an Excel .xlsx file it is only necessary to
1939+
specify a target file name. To write to multiple sheets it is necessary to
1940+
create an `ExcelWriter` object with a target file name, and specify a sheet
1941+
in the file to write to.
1942+
1943+
Multiple sheets may be written to by specifying unique `sheet_name`.
1944+
With all data written to the file it is necessary to save the changes.
1945+
Note that creating an `ExcelWriter` object with a file name that already
1946+
exists will result in the contents of the existing file being erased.
1947+
1948+
Parameters
1949+
----------
1950+
excel_writer : str or ExcelWriter object
1951+
File path or existing ExcelWriter.
1952+
sheet_name : str, default 'Sheet1'
1953+
Name of sheet which will contain DataFrame.
1954+
na_rep : str, default ''
1955+
Missing data representation.
1956+
float_format : str, optional
1957+
Format string for floating point numbers. For example
1958+
``float_format="%.2f"`` will format 0.1234 to 0.12.
1959+
columns : sequence or list of str, optional
1960+
Columns to write.
1961+
header : bool or list of str, default True
1962+
Write out the column names. If a list of string is given it is
1963+
assumed to be aliases for the column names.
1964+
index : bool, default True
1965+
Write row names (index).
1966+
index_label : str or sequence, optional
1967+
Column label for index column(s) if desired. If not specified, and
1968+
`header` and `index` are True, then the index names are used. A
1969+
sequence should be given if the DataFrame uses MultiIndex.
1970+
startrow : int, default 0
1971+
Upper left cell row to dump data frame.
1972+
startcol : int, default 0
1973+
Upper left cell column to dump data frame.
1974+
engine : str, optional
1975+
Write engine to use, 'openpyxl' or 'xlsxwriter'. You can also set this
1976+
via the options ``io.excel.xlsx.writer``, ``io.excel.xls.writer``, and
1977+
``io.excel.xlsm.writer``.
1978+
merge_cells : bool, default True
1979+
Write MultiIndex and Hierarchical Rows as merged cells.
1980+
encoding : str, optional
1981+
Encoding of the resulting excel file. Only necessary for xlwt,
1982+
other writers support unicode natively.
1983+
inf_rep : str, default 'inf'
1984+
Representation for infinity (there is no native representation for
1985+
infinity in Excel).
1986+
verbose : bool, default True
1987+
Display more information in the error logs.
1988+
freeze_panes : tuple of int (length 2), optional
1989+
Specifies the one-based bottommost row and rightmost column that
1990+
is to be frozen.
1991+
1992+
See Also
1993+
--------
1994+
to_csv : Write DataFrame to a comma-separated values (csv) file.
1995+
ExcelWriter : Class for writing DataFrame objects into excel sheets.
1996+
read_excel : Read an Excel file into a pandas DataFrame.
1997+
read_csv : Read a comma-separated values (csv) file into DataFrame.
1998+
1999+
Notes
2000+
-----
2001+
For compatibility with :meth:`~DataFrame.to_csv`,
2002+
to_excel serializes lists and dicts to strings before writing.
2003+
2004+
Once a workbook has been saved it is not possible write further data
2005+
without rewriting the whole workbook.
2006+
2007+
Examples
2008+
--------
2009+
2010+
Create, write to and save a workbook:
2011+
2012+
>>> df1 = pd.DataFrame([['a', 'b'], ['c', 'd']],
2013+
... index=['row 1', 'row 2'],
2014+
... columns=['col 1', 'col 2'])
2015+
>>> df1.to_excel("output.xlsx") # doctest: +SKIP
2016+
2017+
To specify the sheet name:
2018+
2019+
>>> df1.to_excel("output.xlsx",
2020+
... sheet_name='Sheet_name_1') # doctest: +SKIP
2021+
2022+
If you wish to write to more than one sheet in the workbook, it is
2023+
necessary to specify an ExcelWriter object:
2024+
2025+
>>> df2 = df1.copy()
2026+
>>> with pd.ExcelWriter('output.xlsx') as writer: # doctest: +SKIP
2027+
... df1.to_excel(writer, sheet_name='Sheet_name_1')
2028+
... df2.to_excel(writer, sheet_name='Sheet_name_2')
2029+
2030+
ExcelWriter can also be used to append to an existing Excel file:
2031+
2032+
>>> with pd.ExcelWriter('output.xlsx',
2033+
... mode='a') as writer: # doctest: +SKIP
2034+
... df.to_excel(writer, sheet_name='Sheet_name_3')
2035+
2036+
To set the library that is used to write the Excel file,
2037+
you can pass the `engine` keyword (the default engine is
2038+
automatically chosen depending on the file extension):
2039+
2040+
>>> df1.to_excel('output1.xlsx', engine='xlsxwriter') # doctest: +SKIP
2041+
"""
2042+
20452043
df = self if isinstance(self, ABCDataFrame) else self.to_frame()
20462044

20472045
from pandas.io.formats.excel import ExcelFormatter

pandas/io/formats/style.py

+3-14
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@
2727
from pandas._libs import lib
2828
from pandas._typing import Axis, FrameOrSeries, FrameOrSeriesUnion, Label
2929
from pandas.compat._optional import import_optional_dependency
30-
from pandas.util._decorators import Appender
30+
from pandas.util._decorators import doc
3131

3232
from pandas.core.dtypes.common import is_float
3333

3434
import pandas as pd
3535
from pandas.api.types import is_dict_like, is_list_like
3636
import pandas.core.common as com
3737
from pandas.core.frame import DataFrame
38-
from pandas.core.generic import _shared_docs
38+
from pandas.core.generic import NDFrame
3939
from pandas.core.indexing import _maybe_numeric_slice, _non_reducing_slice
4040

4141
jinja2 = import_optional_dependency("jinja2", extra="DataFrame.style requires jinja2.")
@@ -192,18 +192,7 @@ def _repr_html_(self) -> str:
192192
"""
193193
return self.render()
194194

195-
@Appender(
196-
_shared_docs["to_excel"]
197-
% dict(
198-
axes="index, columns",
199-
klass="Styler",
200-
axes_single_arg="{0 or 'index', 1 or 'columns'}",
201-
optional_by="""
202-
by : str or list of str
203-
Name or list of names which refer to the axis items.""",
204-
versionadded_to_excel="\n .. versionadded:: 0.20",
205-
)
206-
)
195+
@doc(NDFrame.to_excel, klass="Styler")
207196
def to_excel(
208197
self,
209198
excel_writer,

0 commit comments

Comments
 (0)