Skip to content

BUG: Correct ExcelWriter #472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pandas-stubs/io/excel/_base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class ExcelWriter:
def __init__(
self,
path: FilePath | WriteExcelBuffer | ExcelWriter,
engine: Literal["auto", "openpyxl", "pyxlsb", "odf"] | None = ...,
engine: Literal["auto", "openpyxl", "odf", "xlsxwriter"] | None = ...,
date_format: str | None = ...,
datetime_format: str | None = ...,
mode: Literal["w", "a"] = ...,
Expand All @@ -115,11 +115,11 @@ class ExcelWriter:
@property
def supported_extensions(self) -> tuple[str, ...]: ...
@property
def engine(self) -> Literal["openpyxl", "pyxlsb", "odf"]: ...
def engine(self) -> Literal["openpyxl", "odf", "xlsxwriter"]: ...
@property
def sheets(self) -> dict[str, Any]: ...
@property
def book(self) -> Workbook | OpenDocument | pyxlsb.workbook.Workbook: ...
def book(self) -> Workbook | OpenDocument: ...
@property
def date_format(self) -> str: ...
@property
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ tables = { version = ">=3.7.0", python = "<3.11" }
lxml = { version = ">=4.7.1,<4.9.0", python = "<3.11" }
pyreadstat = ">=1.2.0"
xlrd = ">=2.0.1"
pyxlsb = ">=1.0.9"
xlsxwriter = ">=3.0.3"
pyxlsb = ">=1.0.10"
odfpy = ">=1.4.1"
xarray = ">=22.6.0"
tabulate = ">=0.8.10"
Expand Down
39 changes: 37 additions & 2 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Dict,
Generator,
List,
Literal,
Union,
)

Expand Down Expand Up @@ -694,12 +695,46 @@ def test_excel_writer():
check(assert_type(ef.close(), None), type(None))


def test_excel_writer_engine():
with ensure_clean(".xlsx") as path:
with pd.ExcelWriter(path, engine="auto") as ew:
check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter)
DF.to_excel(ew, sheet_name="A")

with ensure_clean(".xlsx") as path:
with pd.ExcelWriter(path, engine="openpyxl") as ew:
check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter)
DF.to_excel(ew, sheet_name="A")
check(
assert_type(ew.engine, Literal["openpyxl", "odf", "xlsxwriter"]),
str,
)

with ensure_clean(".ods") as path:
with pd.ExcelWriter(path, engine="odf") as ew:
check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter)
DF.to_excel(ew, sheet_name="A")
check(
assert_type(ew.engine, Literal["openpyxl", "odf", "xlsxwriter"]),
str,
)

with ensure_clean(".xlsx") as path:
with pd.ExcelWriter(path, engine="xlsxwriter") as ew:
check(assert_type(ew, pd.ExcelWriter), pd.ExcelWriter)
DF.to_excel(ew, sheet_name="A")
check(
assert_type(ew.engine, Literal["openpyxl", "odf", "xlsxwriter"]),
str,
)


def test_excel_writer_append_mode():
with ensure_clean(".xlsx") as path:
with pd.ExcelWriter(path, mode="w") as ew:
DF.to_excel(ew, sheet_name="A")
with pd.ExcelWriter(path, mode="a") as ew:
pass
with pd.ExcelWriter(path, mode="a", engine="openpyxl") as ew:
DF.to_excel(ew, sheet_name="B")


def test_to_string():
Expand Down