Skip to content
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

ENH: Drop list menu lost when reading xlsx files from pandas #59593

Closed
1 of 3 tasks
TomHsiung opened this issue Aug 24, 2024 · 3 comments
Closed
1 of 3 tasks

ENH: Drop list menu lost when reading xlsx files from pandas #59593

TomHsiung opened this issue Aug 24, 2024 · 3 comments
Labels
Closing Candidate May be closeable, needs more eyeballs Enhancement IO Excel read_excel, to_excel

Comments

@TomHsiung
Copy link

TomHsiung commented Aug 24, 2024

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

import pandas as pd
import numpy as np
from openpyxl.worksheet.datavalidation import DataValidation
from openpyxl import load_workbook

Create a table by pandas

month = [7, 7, 7]
date = [27, 27, 27]
amount = [1.2, 1.99, 60.8]
kind = ['Meat', 'Meat', 'Meat']
quarter = ['3', '3', '3']
df = pd.DataFrame({
'Month': month,
'Day': date,
'Cost': amount,
'Category': kind,
'Quarter': quarter
})
df.info()
print(df.head(10))
print(len(df))
df.to_excel('~/Downloads/food_1.xlsx', index = False) #save manipulated table to a Excel file

Open the table by openpyxl and add modifications (drop-down list menu)

wb = load_workbook('/Users/username/Downloads/food_1.xlsx')
ws = wb.active
dv1 = DataValidation(type = "list", formula1 = '"Care, Computer, Convenience, Fruit, Housework, Meat, Others, Restaurant, Veg"', allow_blank = True)
dv2 = DataValidation(type = "list", formula1 = '"1, 2, 3, 4"', allow_blank = True)
ws.add_data_validation(dv1)
ws.add_data_validation(dv2)
dv1.add('d2:d51')
dv2.add('e2:e51')
wb.save('/Users/username/Downloads/food_2.xlsx')

I write data into a new xlsx table via pandas. Later, I add drop list menu function by openpyxl. But, if I want to add more rows later by pandas again, like this,

import pandas as pd
df = pd.read_excel('~/Downloads/Predict.xlsx')

The pre-defined drop list menu function is lost, after the edited xlsx file is saved by pandas.

Feature Description

Maybe it is a reasonable function to add pandas's self features to support drop-down list menu.

Alternative Solutions

Or, maybe it is reasonable to enable pandas's support for the drop-down list menu function of openpyxl

Additional Context

Thanks!

@TomHsiung TomHsiung added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 24, 2024
@rhshadrach
Copy link
Member

Thanks for the request. I do not think pandas can support Excel features like this in maintainable manner. I recommend having a function to add the drop-down list after writing to the Excel file if it needs to happen multiple times.

@rhshadrach rhshadrach added IO Excel read_excel, to_excel Closing Candidate May be closeable, needs more eyeballs and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 24, 2024
@TomHsiung
Copy link
Author

TomHsiung commented Aug 25, 2024

Thank you for your update, pal. Yep, there are some methods to achieve the goal. I was thinking of a more native way, using only pandas to do this. Anyway, many thanks for your clarification.

import pandas as pd
import numpy as np
from openpyxl import load_workbook
from openpyxl.utils.dataframe import dataframe_to_rows

Load existing excel file

wb = load_workbook('/Users/username/food_1Aug2024.xlsx')
ws = wb.active

Add new rows via pandas

new_rows = pd.DataFrame({
'Month': [8, 8, 8, 8, 8],
'Day': [7, 7, 7, 7, 7],
'Cost': [20.8, 18.99, 3.99, 35.48, 2.98],
'Category': ['Care', 'Meat', 'Veg', 'Meat', 'Veg'],
'Quarter': [3, 3, 3, 3, 3],
})

Convert the DataFrame to rows of data

for r in dataframe_to_rows(new_rows, index = False, header = False):
ws.append(r)

Save updated data to a new excel file

wb.save('/Users/username/food_7Aug2024.xlsx')

@mroeschke
Copy link
Member

Agreed that it wouldn't be suitable to maintain this feature in pandas so closing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Closing Candidate May be closeable, needs more eyeballs Enhancement IO Excel read_excel, to_excel
Projects
None yet
Development

No branches or pull requests

3 participants