Skip to content
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: 2 additions & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ Formerly Styler relied on ``display.html.use_mathjax``, which has now been repla

There are also bug fixes and deprecations listed below.

Validation now for ``caption`` arg (:issue:`43368`)

.. _whatsnew_140.enhancements.pyarrow_csv_engine:

Multithreaded CSV reading with a new CSV Engine based on pyarrow
Expand Down
10 changes: 10 additions & 0 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -1773,6 +1773,16 @@ def set_caption(self, caption: str | tuple) -> Styler:
-------
self : Styler
"""
msg = "`caption` must be either a string or 2-tuple of strings."
if isinstance(caption, tuple):
if (
len(caption) != 2
or not isinstance(caption[0], str)
or not isinstance(caption[1], str)
):
raise ValueError(msg)
elif not isinstance(caption, str):
raise ValueError(msg)
self.caption = caption
return self

Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/io/formats/style/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -1439,3 +1439,10 @@ def test_hidden_column_names(mi_df):
ctx = mi_styler._translate(True, True)
assert len(ctx["head"]) == 1 # no index names and only one visible column headers
assert ctx["head"][0][1]["display_value"] == " "


@pytest.mark.parametrize("caption", [1, ("a", "b", "c"), (1, "s")])
def test_caption_raises(mi_styler, caption):
msg = "`caption` must be either a string or 2-tuple of strings."
with pytest.raises(ValueError, match=msg):
mi_styler.set_caption(caption)