Skip to content

Commit 0c12f3a

Browse files
committed
Base implementation
1 parent 02d7d0c commit 0c12f3a

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

pandas/core/generic.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3380,6 +3380,9 @@ def to_latex(
33803380
if multirow is None:
33813381
multirow = config.get_option("display.latex.multirow")
33823382

3383+
if column_format is not None and not isinstance(column_format, str):
3384+
raise ValueError("`column_format` must be str or unicode")
3385+
33833386
# Refactor formatters/float_format/decimal/na_rep/escape to Styler structure
33843387
base_format_ = {
33853388
"na_rep": na_rep,
@@ -3421,8 +3424,9 @@ def _wrap(x, alt_format_):
34213424
formatters = functools.partial(_wrap, alt_format_=lambda v: v)
34223425
else:
34233426
formatters = None
3427+
format_index_ = [index_format_, column_format_]
34243428

3425-
hide, relabel = [], []
3429+
hide, relabel_index = [], []
34263430
if columns:
34273431
hide.append(
34283432
{
@@ -3433,7 +3437,8 @@ def _wrap(x, alt_format_):
34333437
if header is False:
34343438
hide.append({"axis": "columns"})
34353439
elif isinstance(header, (list, tuple)):
3436-
relabel = {"labels": header, "axis": "columns"}
3440+
relabel_index = {"labels": header, "axis": "columns"}
3441+
format_index_ = [index_format_] # column_format is overwritten
34373442

34383443
if index is False:
34393444
hide.append({"axis": "index"})
@@ -3462,9 +3467,9 @@ def _wrap(x, alt_format_):
34623467
return self._to_latex_via_styler(
34633468
buf,
34643469
hide=hide,
3465-
relabel=relabel,
3470+
relabel_index=relabel_index,
34663471
format={"formatter": formatters, **base_format_},
3467-
format_index=[index_format_, column_format_],
3472+
format_index=format_index_,
34683473
render_kwargs=render_kwargs,
34693474
)
34703475

@@ -3473,7 +3478,7 @@ def _to_latex_via_styler(
34733478
buf=None,
34743479
*,
34753480
hide: dict | list[dict] | None = None,
3476-
relabel: dict | list[dict] | None = None,
3481+
relabel_index: dict | list[dict] | None = None,
34773482
format: dict | list[dict] | None = None,
34783483
format_index: dict | list[dict] | None = None,
34793484
render_kwargs: dict = {},
@@ -3484,6 +3489,7 @@ def _to_latex_via_styler(
34843489
.. code-block:: python
34853490
styler = Styler(DataFrame)
34863491
styler.hide(**hide)
3492+
styler.relabel_index(**relabel_index)
34873493
styler.format(**format)
34883494
styler.format_index(**format_index)
34893495
styler.to_latex(buf=buf, **render_kwargs)
@@ -3494,7 +3500,7 @@ def _to_latex_via_styler(
34943500
hide : dict, list of dict
34953501
Keyword args to pass to the method call of ``Styler.hide``. If a list will
34963502
call the method numerous times.
3497-
relabel : dict, list of dict
3503+
relabel_index : dict, list of dict
34983504
Keyword args to pass to the method of ``Styler.relabel_index``. If a list
34993505
will call the method numerous times.
35003506
format : dict, list of dict
@@ -3540,7 +3546,7 @@ def _to_latex_via_styler(
35403546
self = cast("DataFrame", self)
35413547
styler = Styler(self, uuid="")
35423548

3543-
for kw_name in ["hide", "relabel", "format", "format_index"]:
3549+
for kw_name in ["hide", "relabel_index", "format", "format_index"]:
35443550
kw = vars()[kw_name]
35453551
if isinstance(kw, dict):
35463552
getattr(styler, kw_name)(**kw)

0 commit comments

Comments
 (0)