-
-
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 7 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 |
---|---|---|
|
@@ -666,6 +666,17 @@ class ExcelWriter(metaclass=abc.ABCMeta): | |
be parsed by ``fsspec``, e.g., starting "s3://", "gcs://". | ||
|
||
.. versionadded:: 1.2.0 | ||
if_sheet_exists : {'new', 'replace', 'overwrite', 'fail'}, default 'new' | ||
How to behave when trying to write to a sheet that already | ||
exists (append mode only). | ||
|
||
* new: Create a new sheet with a different name. | ||
* replace: Delete the contents of the sheet before writing to it. | ||
rhshadrach marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* overwrite: Write directly to the named sheet | ||
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 is the use case for overwrite? 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. Published statistics, at least in the UK, often have sheets which combine headings, date, description, data quality notes, etc with data tables. To automate something like this you would probably have a pre-written template and then write your data from pandas into specific sheets at specific locations. An example I happened to be looking at recently, England's daily, weekly and monthly vaccination figures. 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. @mrob95 - does the current implementation in this PR overwrite the template formatting? E.g. if a column in the template is formatted to percent and I have a DataFrame with 0.5, will it be displayed in excel as 50%? 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. @rhshadrach It doesn't overwrite cell formatting unless there is an alternative style set. E.g. The only exception to this I can see is headers and indexes, which have a hardcoded style which I think will always overwrite certain formats (see Between this and pandas own excel formatting options I think the options are pretty good for styling tables written using |
||
without deleting the previous contents. | ||
* fail: raise a ValueError. | ||
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. rename to 'error' 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. Will do |
||
|
||
.. versionadded:: 1.3.0 | ||
|
||
Attributes | ||
---------- | ||
|
@@ -834,6 +845,7 @@ def __init__( | |
datetime_format=None, | ||
mode: str = "w", | ||
storage_options: StorageOptions = None, | ||
if_sheet_exists: Optional[str] = None, | ||
**engine_kwargs, | ||
): | ||
# validate that this engine can handle the extension | ||
|
@@ -868,6 +880,15 @@ def __init__( | |
|
||
self.mode = mode | ||
|
||
ise_valid = [None, "new", "replace", "overwrite", "fail"] | ||
if if_sheet_exists not in ise_valid: | ||
raise ValueError(f"'{if_sheet_exists}' is not valid for if_sheet_exists") | ||
if if_sheet_exists and "r+" not in mode: | ||
raise ValueError("if_sheet_exists is only valid in append mode (mode='a')") | ||
if if_sheet_exists is None and "r+" in 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. why is the 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. i guess not a big deal 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. You're right, fixed |
||
if_sheet_exists = "new" | ||
self.if_sheet_exists = if_sheet_exists | ||
|
||
def __fspath__(self): | ||
return getattr(self.handles.handle, "name", "") | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is the default not 'error'?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mainly to preserve the current behaviour as the default. Happy to change