Skip to content

Conversation

@jarent-nvidia
Copy link
Contributor

@jarent-nvidia jarent-nvidia commented Nov 5, 2025

Description of the change:
Added boolean 'autofilter' optional argument to 'DataFrame.to_excel' function to add automatic filters to all columns in the exported excel files. As discussed with @WillAyd starting simple by allowing to add filters only to all or none columns.
New option supported by 'xlsxwriter' and 'openpyxl' engines.

@jarent-nvidia jarent-nvidia marked this pull request as draft November 5, 2025 18:14
Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small comment but overall looks like a really nice PR. @rhshadrach any thoughts?

Convert 0-based column index to Excel column name.
"""
column_name = ""
while index > 0 or not column_name:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you raise when index < 0? Also, what's the point of this being a while loop?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignore my while loop comment - I supposed its so you can build column references like AA, BA, etc...

So makes sense, but still in need of a cleanup here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WillAyd Thanks for the feedback - input validation added.

@jarent-nvidia jarent-nvidia marked this pull request as ready for review November 5, 2025 21:19
ws = wb.active

assert ws.auto_filter.ref is not None
print(ws.auto_filter.ref)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove the print statement?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Print statement removed - thanks for checking!

Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm - let's see what others think

Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed this is looking very good. Can you add tests for:

  • index=True
  • nonzero startrow / startcol
  • A DataFrame with MultiIndex columns (also called a hierarchical columns) with both merge_cells=True and merge_cells=False.

startrow: int = 0,
startcol: int = 0,
freeze_panes: tuple[int, int] | None = None,
autofilter_range: str | None = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me we should be raising an error here if a user specifies autofilter=True and it is not supported with an engine rather than silently ignore the user's specification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Enhancement IO Excel read_excel, to_excel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ENH: adding a filter (and bold) to header when writing to excel

3 participants