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

fix(python): Fix use of "hidden_columns" parameter in write_excel #19029

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3121,8 +3121,9 @@ def write_excel(
If the table has headers, provide autofilter capability.
autofit : bool
Calculate individual column widths from the data.
hidden_columns : list
A list or selector representing table columns to hide in the worksheet.
hidden_columns : str | list
A column name, list of column names, or a selector representing table
columns to mark as hidden in the output worksheet.
hide_gridlines : bool
Do not display any gridlines on the output worksheet.
sheet_zoom : int
Expand Down Expand Up @@ -3442,10 +3443,15 @@ def write_excel(
include_header=include_header,
format_cache=fmt_cache,
)

# additional column-level properties
if hidden_columns is None:
hidden_columns = ()
hidden_columns = _expand_selectors(df, hidden_columns)
hidden = set()
elif isinstance(hidden_columns, str):
hidden = {hidden_columns}
else:
hidden = set(_expand_selectors(df, hidden_columns))

if isinstance(column_widths, int):
column_widths = dict.fromkeys(df.columns, column_widths)
else:
Expand All @@ -3455,9 +3461,8 @@ def write_excel(
column_widths = _unpack_multi_column_dict(column_widths or {}) # type: ignore[assignment]

for column in df.columns:
col_idx, options = table_start[1] + df.get_column_index(column), {}
if column in hidden_columns:
options = {"hidden": True}
options = {"hidden": True} if column in hidden else {}
col_idx = table_start[1] + df.get_column_index(column)
if column in column_widths: # type: ignore[operator]
ws.set_column_pixels(
col_idx,
Expand All @@ -3466,6 +3471,8 @@ def write_excel(
None,
options,
)
elif options:
ws.set_column(col_idx, col_idx, None, None, options)

# finally, inject any sparklines into the table
for column, params in (sparklines or {}).items():
Expand Down