ENH: Added autofilter parameter to Excel output for both xlsxwriter and openpyxl engines by Upabjojr · Pull Request #42560 · pandas-dev/pandas (original) (raw)

This feature is analogous to freeze_panes and perfectly fits together.

I find this reasoning compelling. But it is also compelling for the numerous other features of excel, all with slightly different APIs that might or might not be implemented in different engines. Going down this road seems to me like a maintenance nightmare. Instead, I find your reasoning above to make a compelling case to remove freeze_panes.

It is easy for the user to add an autofilter today:

df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})

with pd.ExcelWriter('test.xlsx', engine='openpyxl') as writer:
    df.to_excel(writer, sheet_name='test')
    writer.sheets['test'].auto_filter.ref = "B1:C3"
    

with pd.ExcelWriter('test2.xlsx', engine='xlsxwriter') as writer:
    df.to_excel(writer, sheet_name='test')
    writer.sheets['test'].autofilter(0, 1, 2, 2)

This is why I think we should implement #43088 - make all protected attributes prefixed with _ and make attributes such as sheets public. Numerous third party libraries have already documented using writer.sheets.