-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Allow overwriting existing sheets when appending to excel files #40230
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
Comments
I sympathize, but why is this a pandas issue when the underlying writer's don't support this? |
cc @rhshadrach |
IIUC, pandas is not updating Assuming all that needs to be done here is updating I think the choosing a name similar to DataFrame.to_sql makes sense, but
|
I think this option only really makes sense in append mode, and therefore only with the openpyxl engine. In write mode the user is in complete control so there is no real "existing sheet" problem. Openpyxl supports all of the options I've suggested, the reason pandas does what it does at the moment seems to be just a quirk of the
Those seem reasonable names to me. An alternative to 'avoid' might be 'create' or 'new'? I'm fairly indifferent |
Makes sense - while I don't like adding engine-specific arguments, IMO this is still a clear enough use-case that I'm +1. cc @jreback for any thoughts. I do like 'new' instead of 'avoid'. Also, maybe |
I agree it's not ideal to be adding a bunch of engine-specific options, but I think the functionality is worth it. I can come up with a more detailed motivating example if it's not obvious what kind of thing it would allow. I think |
Is your feature request related to a problem?
As far as I can tell there is no way to write a DataFrame to a specific (named) sheet in an existing Excel file, creating the sheet if it doesn't exist or overwriting if it does. The closest thing to this that exists is the openpyxl append mode, but if you try to write to
sheet_which_already_exists
with this, the data will be written tosheet_which_already_exists1
. Using write mode overwrites the entire book.Being able to overwrite existing sheets would be useful for populating "raw data" sheets in existing reports/publications on a regular basis, with pivot tables etc updating automatically.
This seems to be a very common issue:
https://stackoverflow.com/questions/62618680/overwrite-an-excel-sheet-with-pandas-dataframe-without-affecting-other-sheets
https://stackoverflow.com/questions/47602533/append-a-pandas-dataframe-to-an-existing-excel-table?noredirect=1&lq=1
https://stackoverflow.com/questions/34744863/python-how-to-use-excelwriter-to-write-into-an-existing-worksheet?noredirect=1&lq=1
#28653
#29990
Describe the solution you'd like
There are a few potential behaviours someone might want when they write to an existing sheet in append mode:
Originally I considered adding a flag like
overwrite=True
topd.ExcelWriter
but I don't think this would be sufficient to cover all the potential behaviours, so would suggest an enum along the lines of theif_exists
option indf.to_sql
, with the following options:This option would only be relevant when using
mode="a"
with the openpyxl engine.API breaking implications
The
new_sheet
option would replicate the current behaviour, so this could be made the default.Additional context
Demonstration of current behaviour:
Why does it work this way? At the moment, when the openpyxl
ExcelWriter
is initialised in append mode it loads the workbook, but does not updateself.sheets
to reflect the sheets in the loaded file.https://github.com/pandas-dev/pandas/blob/master/pandas/io/excel/_openpyxl.py#L54-L58
This seems unintentional and means that when
write_cells
is called with a sheet name that already exists in the file, it is not found inself.sheets
:https://github.com/pandas-dev/pandas/blob/master/pandas/io/excel/_openpyxl.py#L414-L419
so we follow the else branch, and when assigning to
wks.title
openpyxl suffixes the sheet name to avoid a naming conflict.The text was updated successfully, but these errors were encountered: