-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
BUG: update dict of sheets before check #27730
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
Conversation
```python import pandas as pd df = pd.DataFrame( { "id": ["1", "2", "3", "4", "5"], "Feature1": ["A", "C", "E", "G", "I"], "Feature2": ["B", "D", "F", "H", "J"], }, columns=["id", "Feature1", "Feature2"], ) writer = pd.ExcelWriter(path="testOutput.xlsx", mode="a", engine="openpyxl") df.to_excel(writer, sheet_name="Main") writer.save() writer.close() ``` #### Problem description I have excel file with existing sheet "Main", and if I try to append some dataframe to this sheet, on first df.to_excel() pandas creates new sheet with name "Main1" and saves information to this new sheet. But if once to_excel(), than pandas add new sheet to dictionary and saves to existing sheet. So, on start appen pandas don't know nothing about existing sheets, and check in **_openpyxl.py** file don't realy work ```python if sheet_name in self.sheets: wks = self.sheets[sheet_name] else: wks = self.book.create_sheet() wks.title = sheet_name self.sheets[sheet_name] = wks ``` so I add new code to update sheetname-list before check.
Add: update dict of sheets before check
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.
does this close an open issue? this would need a test at the very least.
I might not totally understand the issue but append mode allows you to add new sheets to an existing workbook, not necessarily to append data to an existing sheet. The latter is what you are trying to accomplish right? |
Yes, you are right. doesn'y work if sheet exist and creates sheet "Main1", and write to this sheet
And it work fine, when I add data to same sheet second to_excel
on this change it will not create new sheet on 1-st to_sheet() and write DataFrame to existing sheet "Main", and on 2-st to_sheet() write another DataFrame to same sheet. So now it have different behavior on 1-st and 2-nd to_excel() in one function |
If I use function
and add to pandas into current version:
it will print
but it must use (my change):
current behavior: |
I'm indifferent to the change though as mentioned would need tests for review. Right now it's nice that there isn't ambiguity to what append does - it adds a sheet. If we wanted to allow for the updating of an existing sheet we would have to handle the ambiguity of what a user wants (do they want to append a new sheet or add to an existing one?) and probably want to ensure that all excel writers can handle consistently |
Now we have ambiguity to what append does:
If it always creates new sheet -- then there would not be any ambiguity to what append does - it adds a sheet always. As I understand "append" now work only in openpyxl engine which allow write (append) information to existing workbook. |
Taking another look at this it appears this code is just copy / pasted across the openpyxl, xlwt and xlsxwriter modules, but |
Closing as stable but @Zoynels ping if you'd like to pick back up and can reopen |
Problem description
I have excel file with existing sheet "Main", and if I try to append some dataframe to this sheet, on first df.to_excel() pandas creates new sheet with name "Main1" and saves information to this new sheet. But if once to_excel(), than pandas add new sheet to dictionary and saves to existing sheet. So, on start appen pandas don't know nothing about existing sheets, and check in _openpyxl.py file don't realy work
so I add new code to update sheetname-list before check.
black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff