|
1 | 1 | from pathlib import Path
|
| 2 | +import re |
2 | 3 |
|
3 | 4 | import numpy as np
|
4 | 5 | import pytest
|
@@ -109,6 +110,66 @@ def test_write_append_mode(ext, mode, expected):
|
109 | 110 | assert wb2.worksheets[index]["A1"].value == cell_value
|
110 | 111 |
|
111 | 112 |
|
| 113 | +@pytest.mark.parametrize( |
| 114 | + "if_sheet_exists,num_sheets,expected", |
| 115 | + [ |
| 116 | + ("new", 2, ["apple", "banana"]), |
| 117 | + ("replace", 1, ["pear"]), |
| 118 | + ], |
| 119 | +) |
| 120 | +def test_if_sheet_exists_append_modes(ext, if_sheet_exists, num_sheets, expected): |
| 121 | + # GH 40230 |
| 122 | + df1 = DataFrame({"fruit": ["apple", "banana"]}) |
| 123 | + df2 = DataFrame({"fruit": ["pear"]}) |
| 124 | + |
| 125 | + with tm.ensure_clean(ext) as f: |
| 126 | + df1.to_excel(f, engine="openpyxl", sheet_name="foo", index=False) |
| 127 | + with ExcelWriter( |
| 128 | + f, engine="openpyxl", mode="a", if_sheet_exists=if_sheet_exists |
| 129 | + ) as writer: |
| 130 | + df2.to_excel(writer, sheet_name="foo", index=False) |
| 131 | + |
| 132 | + wb = openpyxl.load_workbook(f) |
| 133 | + assert len(wb.sheetnames) == num_sheets |
| 134 | + assert wb.sheetnames[0] == "foo" |
| 135 | + result = pd.read_excel(wb, "foo", engine="openpyxl") |
| 136 | + assert list(result["fruit"]) == expected |
| 137 | + if len(wb.sheetnames) == 2: |
| 138 | + result = pd.read_excel(wb, wb.sheetnames[1], engine="openpyxl") |
| 139 | + tm.assert_frame_equal(result, df2) |
| 140 | + wb.close() |
| 141 | + |
| 142 | + |
| 143 | +@pytest.mark.parametrize( |
| 144 | + "if_sheet_exists,msg", |
| 145 | + [ |
| 146 | + ( |
| 147 | + "invalid", |
| 148 | + "'invalid' is not valid for if_sheet_exists. Valid options " |
| 149 | + "are 'error', 'new' and 'replace'.", |
| 150 | + ), |
| 151 | + ( |
| 152 | + "error", |
| 153 | + "Sheet 'foo' already exists and if_sheet_exists is set to 'error'.", |
| 154 | + ), |
| 155 | + ( |
| 156 | + None, |
| 157 | + "Sheet 'foo' already exists and if_sheet_exists is set to 'error'.", |
| 158 | + ), |
| 159 | + ], |
| 160 | +) |
| 161 | +def test_if_sheet_exists_raises(ext, if_sheet_exists, msg): |
| 162 | + # GH 40230 |
| 163 | + df = DataFrame({"fruit": ["pear"]}) |
| 164 | + with tm.ensure_clean(ext) as f: |
| 165 | + with pytest.raises(ValueError, match=re.escape(msg)): |
| 166 | + df.to_excel(f, "foo", engine="openpyxl") |
| 167 | + with ExcelWriter( |
| 168 | + f, engine="openpyxl", mode="a", if_sheet_exists=if_sheet_exists |
| 169 | + ) as writer: |
| 170 | + df.to_excel(writer, sheet_name="foo") |
| 171 | + |
| 172 | + |
112 | 173 | def test_to_excel_with_openpyxl_engine(ext):
|
113 | 174 | # GH 29854
|
114 | 175 | with tm.ensure_clean(ext) as filename:
|
@@ -175,7 +236,9 @@ def test_append_mode_file(ext):
|
175 | 236 | with tm.ensure_clean(ext) as f:
|
176 | 237 | df.to_excel(f, engine="openpyxl")
|
177 | 238 |
|
178 |
| - with ExcelWriter(f, mode="a", engine="openpyxl") as writer: |
| 239 | + with ExcelWriter( |
| 240 | + f, mode="a", engine="openpyxl", if_sheet_exists="new" |
| 241 | + ) as writer: |
179 | 242 | df.to_excel(writer)
|
180 | 243 |
|
181 | 244 | # make sure that zip files are not concatenated by making sure that
|
|
0 commit comments