Skip to content

Commit

Permalink
fix(python): Consider the original dtypes when selecting columns in t…
Browse files Browse the repository at this point in the history
…he `write_excel` function
  • Loading branch information
arnabanimesh authored Jan 25, 2025
1 parent 1993d59 commit ce4a3bf
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3473,6 +3473,12 @@ def write_excel(
wb, ws, can_close = _xl_setup_workbook(workbook, worksheet)
df, is_empty = self, not len(self)

# The _xl_setup_table_columns function in the below section
# converts all collection types (e.g. List, Struct, Object) to strings
# Hence, we need to store the original schema so that it can be used
# when selecting columns using column selectors based on datatypes
df_ori_dtypes = df.head(0)

# setup table format/columns
fmt_cache = _XLFormatCache(wb)
column_formats = column_formats or {}
Expand Down Expand Up @@ -3540,7 +3546,7 @@ def write_excel(
elif isinstance(hidden_columns, str):
hidden = {hidden_columns}
else:
hidden = set(_expand_selectors(df, hidden_columns))
hidden = set(_expand_selectors(df_ori_dtypes, hidden_columns))

# Autofit section needs to be present above column_widths section
# to ensure that parameters provided in the column_widths section
Expand All @@ -3558,7 +3564,7 @@ def write_excel(
column_widths = dict.fromkeys(df.columns, column_widths)
else:
column_widths = _expand_selector_dicts( # type: ignore[assignment]
df, column_widths, expand_keys=True, expand_values=False
df_ori_dtypes, column_widths, expand_keys=True, expand_values=False
)
column_widths = _unpack_multi_column_dict(column_widths or {}) # type: ignore[assignment]

Expand Down

0 comments on commit ce4a3bf

Please sign in to comment.