Skip to content

Commit 71104f6

Browse files
committed
Update avoid -> new in ExcelWriter if_sheet_exists
1 parent 7d4e93d commit 71104f6

File tree

4 files changed

+13
-13
lines changed

4 files changed

+13
-13
lines changed

pandas/io/excel/_base.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -666,11 +666,11 @@ class ExcelWriter(metaclass=abc.ABCMeta):
666666
be parsed by ``fsspec``, e.g., starting "s3://", "gcs://".
667667
668668
.. versionadded:: 1.2.0
669-
if_sheet_exists : {'avoid', 'replace', 'overwrite', 'fail'}, default 'avoid'
669+
if_sheet_exists : {'new', 'replace', 'overwrite', 'fail'}, default 'new'
670670
How to behave when trying to write to a sheet that already
671671
exists (append mode only).
672672
673-
* avoid: Create a new sheet with a different name.
673+
* new: Create a new sheet with a different name.
674674
* replace: Delete the contents of the sheet before writing to it.
675675
* overwrite: Write directly to the named sheet
676676
without deleting the previous contents.
@@ -880,17 +880,13 @@ def __init__(
880880

881881
self.mode = mode
882882

883+
ise_valid = [None, "new", "replace", "overwrite", "fail"]
884+
if if_sheet_exists not in ise_valid:
885+
raise ValueError(f"'{if_sheet_exists}' is not valid for if_sheet_exists")
883886
if if_sheet_exists and "r+" not in mode:
884887
raise ValueError("if_sheet_exists is only valid in append mode (mode='a')")
885-
if if_sheet_exists is not None and if_sheet_exists not in {
886-
"avoid",
887-
"replace",
888-
"overwrite",
889-
"fail",
890-
}:
891-
raise ValueError(f"'{if_sheet_exists}' is not valid for if_sheet_exists")
892888
if if_sheet_exists is None and "r+" in mode:
893-
if_sheet_exists = "avoid"
889+
if_sheet_exists = "new"
894890
self.if_sheet_exists = if_sheet_exists
895891

896892
def __fspath__(self):

pandas/io/excel/_openpyxl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ def write_cells(
420420

421421
if sheet_name in self.sheets:
422422
if "r+" in self.mode:
423-
if self.if_sheet_exists == "avoid":
423+
if self.if_sheet_exists in "new":
424424
wks = self.book.create_sheet()
425425
# openpyxl will create a name for the new sheet by appending digits
426426
wks.title = sheet_name

pandas/tests/io/excel/test_openpyxl.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def test_write_append_mode(ext, mode, expected):
113113
@pytest.mark.parametrize(
114114
"if_sheet_exists,num_sheets,expected",
115115
[
116-
("avoid", 2, ["apple", "banana"]),
116+
("new", 2, ["apple", "banana"]),
117117
(None, 2, ["apple", "banana"]),
118118
("replace", 1, ["pear"]),
119119
("overwrite", 1, ["pear", "banana"]),
@@ -152,7 +152,7 @@ def test_if_sheet_exists_raises(ext):
152152

153153
with tm.ensure_clean(ext) as f:
154154
with pytest.raises(ValueError, match=re.escape(mode_msg)):
155-
ExcelWriter(f, engine="openpyxl", mode="w", if_sheet_exists="new_sheet")
155+
ExcelWriter(f, engine="openpyxl", mode="w", if_sheet_exists="new")
156156

157157
with tm.ensure_clean(ext) as f:
158158
with pytest.raises(ValueError, match=invalid_msg):

pandas/tests/io/excel/test_xlsxwriter.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
import warnings
23

34
import pytest
@@ -57,7 +58,10 @@ def test_column_format(ext):
5758

5859
def test_write_append_mode_raises(ext):
5960
msg = "Append mode is not supported with xlsxwriter!"
61+
ise_msg = "if_sheet_exists is only valid in append mode (mode='a')"
6062

6163
with tm.ensure_clean(ext) as f:
6264
with pytest.raises(ValueError, match=msg):
6365
ExcelWriter(f, engine="xlsxwriter", mode="a")
66+
with pytest.raises(ValueError, match=re.escape(ise_msg)):
67+
ExcelWriter(f, engine="xlsxwriter", if_sheet_exists="replace")

0 commit comments

Comments
 (0)