-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: add if_sheet_exists='overlay' to ExcelWriter #42222
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 13 commits
bc6534a
66fcd15
cb59157
b9ed327
211683d
a226668
087274a
9f79b0f
e7362eb
d92f414
d0a4565
54e274d
aa9afbc
ba38c31
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 |
---|---|---|
|
@@ -690,15 +690,23 @@ class ExcelWriter(metaclass=abc.ABCMeta): | |
be parsed by ``fsspec``, e.g., starting "s3://", "gcs://". | ||
|
||
.. versionadded:: 1.2.0 | ||
if_sheet_exists : {'error', 'new', 'replace'}, default 'error' | ||
|
||
if_sheet_exists : {'error', 'new', 'replace', 'overlay'}, default 'error' | ||
How to behave when trying to write to a sheet that already | ||
exists (append mode only). | ||
|
||
* error: raise a ValueError. | ||
* new: Create a new sheet, with a name determined by the engine. | ||
* replace: Delete the contents of the sheet before writing to it. | ||
* overlay: Write contents to the existing sheet without removing the old | ||
contents. | ||
|
||
.. versionadded:: 1.3.0 | ||
|
||
.. versionchanged:: 1.4.0 | ||
|
||
Added ``overlay`` option | ||
|
||
engine_kwargs : dict, optional | ||
Keyword arguments to be passed into the engine. | ||
|
||
|
@@ -764,6 +772,28 @@ class ExcelWriter(metaclass=abc.ABCMeta): | |
>>> with pd.ExcelWriter("path_to_file.xlsx", mode="a", engine="openpyxl") as writer: | ||
... df.to_excel(writer, sheet_name="Sheet3") | ||
|
||
Here, the `if_sheet_exists` parameter can be set to replace a sheet if it | ||
feefladder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
already exists: | ||
feefladder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
>>> with ExcelWriter( | ||
... "path_to_file.xlsx", | ||
... mode="a", | ||
... engine="openpyxl", | ||
... if_sheet_exists="replace", | ||
... ) as writer: | ||
... df.to_excel(writer, sheet_name="Sheet1") | ||
|
||
You can also write multiple DataFrames to a single sheet. Note that the | ||
``if_sheet_exists`` parameter needs to be set to ``overlay``: | ||
|
||
>>> with ExcelWriter("path_to_file.xlsx", | ||
... mode="a", | ||
... engine="openpyxl", | ||
... if_sheet_exists="overlay", | ||
... ) as writer: | ||
... df1.to_excel(writer, sheet_name="Sheet1") | ||
... df2.to_excel(writer, sheet_name="Sheet1", startcol=3) | ||
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. does overlay require startrow to be not 1? (e.g should we error if this is not specified) 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. Without startrow (e.g. 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. This is what I would expect - unspecified starts at the very top left corner of the sheet. |
||
|
||
You can store Excel file in RAM: | ||
|
||
>>> import io | ||
|
@@ -949,10 +979,10 @@ def __init__( | |
|
||
self.mode = mode | ||
|
||
if if_sheet_exists not in [None, "error", "new", "replace"]: | ||
if if_sheet_exists not in (None, "error", "new", "replace", "overlay"): | ||
raise ValueError( | ||
f"'{if_sheet_exists}' is not valid for if_sheet_exists. " | ||
"Valid options are 'error', 'new' and 'replace'." | ||
"Valid options are 'error', 'new', 'replace' and 'overlay'." | ||
) | ||
if if_sheet_exists and "r+" not in mode: | ||
raise ValueError("if_sheet_exists is only valid in append mode (mode='a')") | ||
|
Uh oh!
There was an error while loading. Please reload this page.