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 bool/string usage of "column_totals" parameter in write_excel #17846

Merged
merged 1 commit into from
Jul 25, 2024
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
2 changes: 1 addition & 1 deletion py-polars/polars/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@
]
ColumnTotalsDefinition: TypeAlias = Union[
# dict of colname(s) to str, a collection of str, or a boolean
Mapping[Union[str, Collection[str]], str],
Mapping[Union[ColumnNameOrSelector, Tuple[ColumnNameOrSelector]], str],
Sequence[str],
bool,
]
Expand Down
27 changes: 14 additions & 13 deletions py-polars/polars/io/spreadsheet/_write_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from polars.datatypes.group import FLOAT_DTYPES, INTEGER_DTYPES
from polars.dependencies import json
from polars.exceptions import DuplicateError
from polars.selectors import _expand_selector_dicts, _expand_selectors
from polars.selectors import _expand_selector_dicts, _expand_selectors, numeric

if TYPE_CHECKING:
from typing import Literal
Expand Down Expand Up @@ -346,24 +346,30 @@ def _map_str(s: Series) -> Series:
if cast_cols:
df = df.with_columns(cast_cols)

# expand/normalise column totals
if column_totals is True:
column_totals = {numeric(): "sum"}
elif isinstance(column_totals, str):
column_totals = {numeric(): column_totals.lower()}

column_totals = _unpack_multi_column_dict( # type: ignore[assignment]
_expand_selector_dicts(df, column_totals, expand_keys=True, expand_values=False)
if isinstance(column_totals, dict)
else _expand_selectors(df, column_totals)
)
column_formats = _unpack_multi_column_dict( # type: ignore[assignment]
_expand_selector_dicts(
df, column_formats, expand_keys=True, expand_values=False, tuple_keys=True
)
)

# normalise column totals
column_total_funcs = (
{col: "sum" for col in column_totals}
if isinstance(column_totals, Sequence)
else (column_totals.copy() if isinstance(column_totals, dict) else {})
)

# expand/normalise column formats
column_formats = _unpack_multi_column_dict( # type: ignore[assignment]
_expand_selector_dicts(
df, column_formats, expand_keys=True, expand_values=False, tuple_keys=True
)
)

# normalise row totals
if not row_totals:
row_total_funcs = {}
Expand Down Expand Up @@ -444,11 +450,6 @@ def _map_str(s: Series) -> Series:
if base_type in dtype_formats:
fmt = dtype_formats.get(tp, dtype_formats[base_type])
column_formats.setdefault(col, fmt)
if base_type.is_numeric():
if column_totals is True:
column_total_funcs.setdefault(col, "sum")
elif isinstance(column_totals, str):
column_total_funcs.setdefault(col, column_totals.lower())
if col not in column_formats:
column_formats[col] = fmt_default

Expand Down