Skip to content

Commit d8996ad

Browse files
authored
DOC: Fix docs in pandas/io/excel/* (#41286)
1 parent 46714a8 commit d8996ad

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

ci/code_checks.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# $ ./ci/code_checks.sh code # checks on imported code
1616
# $ ./ci/code_checks.sh doctests # run doctests
1717
# $ ./ci/code_checks.sh docstrings # validate docstring errors
18-
# $ ./ci/code_checks.sh typing # run static type analysis
18+
# $ ./ci/code_checks.sh typing # run static type analysis
1919

2020
[[ -z "$1" || "$1" == "lint" || "$1" == "patterns" || "$1" == "code" || "$1" == "doctests" || "$1" == "docstrings" || "$1" == "typing" ]] || \
2121
{ echo "Unknown command $1. Usage: $0 [lint|patterns|code|doctests|docstrings|typing]"; exit 9999; }
@@ -140,6 +140,7 @@ if [[ -z "$CHECK" || "$CHECK" == "doctests" ]]; then
140140
pandas/core/window/ \
141141
pandas/errors/ \
142142
pandas/io/clipboard/ \
143+
pandas/io/excel/ \
143144
pandas/io/parsers/ \
144145
pandas/io/sas/ \
145146
pandas/tseries/

pandas/io/excel/_base.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -707,39 +707,55 @@ class ExcelWriter(metaclass=abc.ABCMeta):
707707
--------
708708
Default usage:
709709
710-
>>> with ExcelWriter('path_to_file.xlsx') as writer:
710+
>>> df = pd.DataFrame([["ABC", "XYZ"]], columns=["Foo", "Bar"])
711+
>>> with ExcelWriter("path_to_file.xlsx") as writer:
711712
... df.to_excel(writer)
712713
713714
To write to separate sheets in a single file:
714715
715-
>>> with ExcelWriter('path_to_file.xlsx') as writer:
716-
... df1.to_excel(writer, sheet_name='Sheet1')
717-
... df2.to_excel(writer, sheet_name='Sheet2')
716+
>>> df1 = pd.DataFrame([["AAA", "BBB"]], columns=["Spam", "Egg"])
717+
>>> df2 = pd.DataFrame([["ABC", "XYZ"]], columns=["Foo", "Bar"])
718+
>>> with ExcelWriter("path_to_file.xlsx") as writer:
719+
... df1.to_excel(writer, sheet_name="Sheet1")
720+
... df2.to_excel(writer, sheet_name="Sheet2")
718721
719722
You can set the date format or datetime format:
720723
721-
>>> with ExcelWriter('path_to_file.xlsx',
722-
... date_format='YYYY-MM-DD',
723-
... datetime_format='YYYY-MM-DD HH:MM:SS') as writer:
724+
>>> from datetime import date, datetime
725+
>>> df = pd.DataFrame(
726+
... [
727+
... [date(2014, 1, 31), date(1999, 9, 24)],
728+
... [datetime(1998, 5, 26, 23, 33, 4), datetime(2014, 2, 28, 13, 5, 13)],
729+
... ],
730+
... index=["Date", "Datetime"],
731+
... columns=["X", "Y"],
732+
... )
733+
>>> with ExcelWriter(
734+
... "path_to_file.xlsx",
735+
... date_format="YYYY-MM-DD",
736+
... datetime_format="YYYY-MM-DD HH:MM:SS"
737+
... ) as writer:
724738
... df.to_excel(writer)
725739
726740
You can also append to an existing Excel file:
727741
728-
>>> with ExcelWriter('path_to_file.xlsx', mode='a') as writer:
729-
... df.to_excel(writer, sheet_name='Sheet3')
742+
>>> with ExcelWriter("path_to_file.xlsx", mode="a", engine="openpyxl") as writer:
743+
... df.to_excel(writer, sheet_name="Sheet3")
730744
731745
You can store Excel file in RAM:
732746
733747
>>> import io
748+
>>> df = pd.DataFrame([["ABC", "XYZ"]], columns=["Foo", "Bar"])
734749
>>> buffer = io.BytesIO()
735750
>>> with pd.ExcelWriter(buffer) as writer:
736751
... df.to_excel(writer)
737752
738753
You can pack Excel file into zip archive:
739754
740755
>>> import zipfile
741-
>>> with zipfile.ZipFile('path_to_file.zip', 'w') as zf:
742-
... with zf.open('filename.xlsx', 'w') as buffer:
756+
>>> df = pd.DataFrame([["ABC", "XYZ"]], columns=["Foo", "Bar"])
757+
>>> with zipfile.ZipFile("path_to_file.zip", "w") as zf:
758+
... with zf.open("filename.xlsx", "w") as buffer:
743759
... with pd.ExcelWriter(buffer) as writer:
744760
... df.to_excel(writer)
745761
"""

0 commit comments

Comments
 (0)