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): Consider the original dtypes when selecting columns in write_excel function #20909

Merged
merged 8 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
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
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_original = df.clear()

# 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_original, 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_original, column_widths, expand_keys=True, expand_values=False
)
column_widths = _unpack_multi_column_dict(column_widths or {}) # type: ignore[assignment]

Expand Down
50 changes: 50 additions & 0 deletions py-polars/tests/unit/io/test_spreadsheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -1350,3 +1350,53 @@ def test_drop_empty_rows(
drop_empty_rows=False,
)
assert df3.shape == (10, 4)


def test_write_excel_select_col_dtype() -> None:
from openpyxl import load_workbook
from xlsxwriter import Workbook

def get_col_widths(wb_bytes: BytesIO) -> dict[str, int]:
return {
k: round(v.width)
for k, v in load_workbook(wb_bytes).active.column_dimensions.items()
}

df = pl.DataFrame(
{
"name": [["Alice", "Ben"], ["Charlie", "Delta"]],
"col2": ["Hi", "Bye"],
}
)

# column_widths test:
# pl.List(pl.String)) datatype should not match column with no list
check = BytesIO()
with Workbook(check) as wb:
df.write_excel(wb, column_widths={cs.by_dtype(pl.List(pl.String)): 300})

assert get_col_widths(check) == {"A": 43}

# column_widths test:
# pl.String datatype should not match column with list
check = BytesIO()
with Workbook(check) as wb:
df.write_excel(wb, column_widths={cs.by_dtype(pl.String): 300})

assert get_col_widths(check) == {"B": 43}

# hidden_columns test:
# pl.List(pl.String)) datatype should not match column with no list
check = BytesIO()
with Workbook(check) as wb:
df.write_excel(wb, hidden_columns=cs.by_dtype(pl.List(pl.String)))

assert get_col_widths(check) == {"A": 0}

# hidden_columns test:
# pl.String datatype should not match column with list
check = BytesIO()
with Workbook(check) as wb:
df.write_excel(wb, hidden_columns=cs.by_dtype(pl.String))

assert get_col_widths(check) == {"B": 0}
Loading