diff --git a/ci/code_checks.sh b/ci/code_checks.sh index 149e10b48933d..8e876eebf93ad 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -15,7 +15,7 @@ # $ ./ci/code_checks.sh code # checks on imported code # $ ./ci/code_checks.sh doctests # run doctests # $ ./ci/code_checks.sh docstrings # validate docstring errors -# $ ./ci/code_checks.sh typing # run static type analysis +# $ ./ci/code_checks.sh typing # run static type analysis [[ -z "$1" || "$1" == "lint" || "$1" == "patterns" || "$1" == "code" || "$1" == "doctests" || "$1" == "docstrings" || "$1" == "typing" ]] || \ { echo "Unknown command $1. Usage: $0 [lint|patterns|code|doctests|docstrings|typing]"; exit 9999; } @@ -140,6 +140,7 @@ if [[ -z "$CHECK" || "$CHECK" == "doctests" ]]; then pandas/core/window/ \ pandas/errors/ \ pandas/io/clipboard/ \ + pandas/io/excel/ \ pandas/io/parsers/ \ pandas/io/sas/ \ pandas/tseries/ diff --git a/pandas/io/excel/_base.py b/pandas/io/excel/_base.py index 3c9dd90c0a0cb..d26a991ba2820 100644 --- a/pandas/io/excel/_base.py +++ b/pandas/io/excel/_base.py @@ -707,30 +707,45 @@ class ExcelWriter(metaclass=abc.ABCMeta): -------- Default usage: - >>> with ExcelWriter('path_to_file.xlsx') as writer: + >>> df = pd.DataFrame([["ABC", "XYZ"]], columns=["Foo", "Bar"]) + >>> with ExcelWriter("path_to_file.xlsx") as writer: ... df.to_excel(writer) To write to separate sheets in a single file: - >>> with ExcelWriter('path_to_file.xlsx') as writer: - ... df1.to_excel(writer, sheet_name='Sheet1') - ... df2.to_excel(writer, sheet_name='Sheet2') + >>> df1 = pd.DataFrame([["AAA", "BBB"]], columns=["Spam", "Egg"]) + >>> df2 = pd.DataFrame([["ABC", "XYZ"]], columns=["Foo", "Bar"]) + >>> with ExcelWriter("path_to_file.xlsx") as writer: + ... df1.to_excel(writer, sheet_name="Sheet1") + ... df2.to_excel(writer, sheet_name="Sheet2") You can set the date format or datetime format: - >>> with ExcelWriter('path_to_file.xlsx', - ... date_format='YYYY-MM-DD', - ... datetime_format='YYYY-MM-DD HH:MM:SS') as writer: + >>> from datetime import date, datetime + >>> df = pd.DataFrame( + ... [ + ... [date(2014, 1, 31), date(1999, 9, 24)], + ... [datetime(1998, 5, 26, 23, 33, 4), datetime(2014, 2, 28, 13, 5, 13)], + ... ], + ... index=["Date", "Datetime"], + ... columns=["X", "Y"], + ... ) + >>> with ExcelWriter( + ... "path_to_file.xlsx", + ... date_format="YYYY-MM-DD", + ... datetime_format="YYYY-MM-DD HH:MM:SS" + ... ) as writer: ... df.to_excel(writer) You can also append to an existing Excel file: - >>> with ExcelWriter('path_to_file.xlsx', mode='a') as writer: - ... df.to_excel(writer, sheet_name='Sheet3') + >>> with ExcelWriter("path_to_file.xlsx", mode="a", engine="openpyxl") as writer: + ... df.to_excel(writer, sheet_name="Sheet3") You can store Excel file in RAM: >>> import io + >>> df = pd.DataFrame([["ABC", "XYZ"]], columns=["Foo", "Bar"]) >>> buffer = io.BytesIO() >>> with pd.ExcelWriter(buffer) as writer: ... df.to_excel(writer) @@ -738,8 +753,9 @@ class ExcelWriter(metaclass=abc.ABCMeta): You can pack Excel file into zip archive: >>> import zipfile - >>> with zipfile.ZipFile('path_to_file.zip', 'w') as zf: - ... with zf.open('filename.xlsx', 'w') as buffer: + >>> df = pd.DataFrame([["ABC", "XYZ"]], columns=["Foo", "Bar"]) + >>> with zipfile.ZipFile("path_to_file.zip", "w") as zf: + ... with zf.open("filename.xlsx", "w") as buffer: ... with pd.ExcelWriter(buffer) as writer: ... df.to_excel(writer) """