Skip to content

ENH: Add option to only merge column header cells in ExcelFormatter. #59081

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

Merged
merged 8 commits into from
Jun 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Other enhancements
- Users can globally disable any ``PerformanceWarning`` by setting the option ``mode.performance_warnings`` to ``False`` (:issue:`56920`)
- :meth:`Styler.format_index_names` can now be used to format the index and column names (:issue:`48936` and :issue:`47489`)
- :class:`.errors.DtypeWarning` improved to include column names when mixed data types are detected (:issue:`58174`)
- :func:`DataFrame.to_excel` argument ``merge_cells`` now accepts a value of ``"columns"`` to only merge :class:`MultiIndex` column header header cells (:issue:`35384`)
- :meth:`DataFrame.corrwith` now accepts ``min_periods`` as optional arguments, as in :meth:`DataFrame.corr` and :meth:`Series.corr` (:issue:`9490`)
- :meth:`DataFrame.cummin`, :meth:`DataFrame.cummax`, :meth:`DataFrame.cumprod` and :meth:`DataFrame.cumsum` methods now have a ``numeric_only`` parameter (:issue:`53072`)
- :meth:`DataFrame.fillna` and :meth:`Series.fillna` can now accept ``value=None``; for non-object dtype the corresponding NA value will be used (:issue:`57723`)
Expand Down
1 change: 1 addition & 0 deletions pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ def closed(self) -> bool:

# ExcelWriter
ExcelWriterIfSheetExists = Literal["error", "new", "replace", "overlay"]
ExcelWriterMergeCells = Union[bool, Literal["columns"]]

# Offsets
OffsetCalendar = Union[np.busdaycalendar, "AbstractHolidayCalendar"]
Expand Down
23 changes: 16 additions & 7 deletions pandas/io/formats/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

if TYPE_CHECKING:
from pandas._typing import (
ExcelWriterMergeCells,
FilePath,
IndexLabel,
StorageOptions,
Expand Down Expand Up @@ -523,8 +524,11 @@ class ExcelFormatter:
Column label for index column(s) if desired. If None is given, and
`header` and `index` are True, then the index names are used. A
sequence should be given if the DataFrame uses MultiIndex.
merge_cells : bool, default False
Format MultiIndex and Hierarchical Rows as merged cells.
merge_cells : bool or 'columns', default False
Format MultiIndex column headers and Hierarchical Rows as merged cells
if True. Merge MultiIndex column headers only if 'columns'.
.. versionchanged:: 3.0.0
Added the 'columns' option.
inf_rep : str, default `'inf'`
representation for np.inf values (which aren't representable in Excel)
A `'-'` sign will be added in front of -inf.
Expand All @@ -547,7 +551,7 @@ def __init__(
header: Sequence[Hashable] | bool = True,
index: bool = True,
index_label: IndexLabel | None = None,
merge_cells: bool = False,
merge_cells: ExcelWriterMergeCells = False,
inf_rep: str = "inf",
style_converter: Callable | None = None,
) -> None:
Expand Down Expand Up @@ -580,6 +584,9 @@ def __init__(
self.index = index
self.index_label = index_label
self.header = header

if not isinstance(merge_cells, bool) and merge_cells != "columns":
raise ValueError(f"Unexpected value for {merge_cells=}.")
self.merge_cells = merge_cells
self.inf_rep = inf_rep

Expand Down Expand Up @@ -614,7 +621,7 @@ def _format_header_mi(self) -> Iterable[ExcelCell]:

columns = self.columns
level_strs = columns._format_multi(
sparsify=self.merge_cells, include_names=False
sparsify=self.merge_cells in {True, "columns"}, include_names=False
)
level_lengths = get_level_lengths(level_strs)
coloffset = 0
Expand All @@ -623,7 +630,7 @@ def _format_header_mi(self) -> Iterable[ExcelCell]:
if self.index and isinstance(self.df.index, MultiIndex):
coloffset = self.df.index.nlevels - 1

if self.merge_cells:
if self.merge_cells in {True, "columns"}:
# Format multi-index as a merged cells.
for lnum, name in enumerate(columns.names):
yield ExcelCell(
Expand Down Expand Up @@ -793,15 +800,17 @@ def _format_hierarchical_rows(self) -> Iterable[ExcelCell]:
# with index names (blank if None) for
# unambiguous round-trip, unless not merging,
# in which case the names all go on one row Issue #11328
if isinstance(self.columns, MultiIndex) and self.merge_cells:
if isinstance(self.columns, MultiIndex) and (
self.merge_cells in {True, "columns"}
):
self.rowcounter += 1

# if index labels are not empty go ahead and dump
if com.any_not_none(*index_labels) and self.header is not False:
for cidx, name in enumerate(index_labels):
yield ExcelCell(self.rowcounter - 1, cidx, name, None)

if self.merge_cells:
if self.merge_cells and self.merge_cells != "columns":
# Format hierarchical rows as merged cells.
level_strs = self.df.index._format_multi(
sparsify=True, include_names=False
Expand Down
3 changes: 2 additions & 1 deletion pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
Axis,
AxisInt,
Concatenate,
ExcelWriterMergeCells,
FilePath,
IndexLabel,
IntervalClosedType,
Expand Down Expand Up @@ -551,7 +552,7 @@ def to_excel(
startrow: int = 0,
startcol: int = 0,
engine: str | None = None,
merge_cells: bool = True,
merge_cells: ExcelWriterMergeCells = True,
encoding: str | None = None,
inf_rep: str = "inf",
verbose: bool = True,
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/excel/test_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def frame(float_frame):
return float_frame[:10]


@pytest.fixture(params=[True, False])
@pytest.fixture(params=[True, False, "columns"])
def merge_cells(request):
return request.param

Expand Down
Loading