-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Add if_sheet_exists parameter to ExcelWriter #40231
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
Changes from all commits
4b73d6a
5bce9ac
e8716c7
7d4e93d
71104f6
ae5b385
6baec67
3a2a54f
fcbab05
5bcd6e3
47527c6
060d754
d8f0be1
26511c7
e31a59e
5ed0fb8
338b6d5
5b13015
b2ad90b
320728a
49c4d3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from pathlib import Path | ||
import re | ||
|
||
import numpy as np | ||
import pytest | ||
|
@@ -109,6 +110,66 @@ def test_write_append_mode(ext, mode, expected): | |
assert wb2.worksheets[index]["A1"].value == cell_value | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"if_sheet_exists,num_sheets,expected", | ||
[ | ||
("new", 2, ["apple", "banana"]), | ||
("replace", 1, ["pear"]), | ||
], | ||
) | ||
def test_if_sheet_exists_append_modes(ext, if_sheet_exists, num_sheets, expected): | ||
# GH 40230 | ||
df1 = DataFrame({"fruit": ["apple", "banana"]}) | ||
df2 = DataFrame({"fruit": ["pear"]}) | ||
|
||
with tm.ensure_clean(ext) as f: | ||
df1.to_excel(f, engine="openpyxl", sheet_name="foo", index=False) | ||
with ExcelWriter( | ||
f, engine="openpyxl", mode="a", if_sheet_exists=if_sheet_exists | ||
) as writer: | ||
df2.to_excel(writer, sheet_name="foo", index=False) | ||
|
||
wb = openpyxl.load_workbook(f) | ||
assert len(wb.sheetnames) == num_sheets | ||
assert wb.sheetnames[0] == "foo" | ||
result = pd.read_excel(wb, "foo", engine="openpyxl") | ||
assert list(result["fruit"]) == expected | ||
if len(wb.sheetnames) == 2: | ||
result = pd.read_excel(wb, wb.sheetnames[1], engine="openpyxl") | ||
tm.assert_frame_equal(result, df2) | ||
wb.close() | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"if_sheet_exists,msg", | ||
[ | ||
( | ||
"invalid", | ||
"'invalid' is not valid for if_sheet_exists. Valid options " | ||
"are 'error', 'new' and 'replace'.", | ||
), | ||
( | ||
"error", | ||
"Sheet 'foo' already exists and if_sheet_exists is set to 'error'.", | ||
), | ||
( | ||
None, | ||
"Sheet 'foo' already exists and if_sheet_exists is set to 'error'.", | ||
), | ||
], | ||
) | ||
def test_if_sheet_exists_raises(ext, if_sheet_exists, msg): | ||
# GH 40230 | ||
df = DataFrame({"fruit": ["pear"]}) | ||
with tm.ensure_clean(ext) as f: | ||
with pytest.raises(ValueError, match=re.escape(msg)): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about other engines? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Openpyxl is the only engine which supports append mode currently, and this option only affects append mode There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, so we completely ignore on other engines is fine. is there a reason to raise / warn in that case if its not None? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As it's written at the moment passing |
||
df.to_excel(f, "foo", engine="openpyxl") | ||
with ExcelWriter( | ||
f, engine="openpyxl", mode="a", if_sheet_exists=if_sheet_exists | ||
) as writer: | ||
df.to_excel(writer, sheet_name="foo") | ||
|
||
|
||
def test_to_excel_with_openpyxl_engine(ext): | ||
# GH 29854 | ||
with tm.ensure_clean(ext) as filename: | ||
|
@@ -175,7 +236,9 @@ def test_append_mode_file(ext): | |
with tm.ensure_clean(ext) as f: | ||
df.to_excel(f, engine="openpyxl") | ||
|
||
with ExcelWriter(f, mode="a", engine="openpyxl") as writer: | ||
with ExcelWriter( | ||
f, mode="a", engine="openpyxl", if_sheet_exists="new" | ||
) as writer: | ||
df.to_excel(writer) | ||
|
||
# make sure that zip files are not concatenated by making sure that | ||
|
Uh oh!
There was an error while loading. Please reload this page.