Skip to content

Commit 643d300

Browse files
committed
styler.format options and validator tests
1 parent a6943ae commit 643d300

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

pandas/core/config_init.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,12 @@ def register_converter_cb(key):
793793
A formatter object to be used as default within ``Styler.format``.
794794
"""
795795

796+
797+
def _is_formatter(x):
798+
if not (x is None or callable(x) or isinstance(x, (dict, str))):
799+
return ValueError("Value must have type 'callable, dict, str' or None.")
800+
801+
796802
with cf.config_prefix("styler"):
797803
cf.register_option("sparse.index", True, styler_sparse_index_doc, validator=bool)
798804

@@ -838,5 +844,5 @@ def register_converter_cb(key):
838844
"format.formatter",
839845
None,
840846
styler_formatter,
841-
validator=is_instance_factory([type(None), dict, callable, str]),
847+
validator=_is_formatter,
842848
)

pandas/tests/io/formats/style/test_format.py

+12
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,15 @@ def test_format_options():
298298
with option_context("styler.format.formatter", {"int": "{:,.2f}"}):
299299
ctx_with_op = df.style._translate(True, True)
300300
assert ctx_with_op["body"][0][1]["display_value"] == "2,000.00"
301+
302+
303+
def test_format_options_validator():
304+
df = DataFrame([[9]])
305+
with option_context("styler.format.formatter", lambda x: f"{x:.3f}"):
306+
assert " 9.000 " in df.style.to_latex()
307+
with option_context("styler.format.formatter", "{:.2f}"):
308+
assert " 9.00 " in df.style.to_latex()
309+
with option_context("styler.format.formatter", {0: "{:.1f}"}):
310+
assert " 9.0 " in df.style.to_latex()
311+
with option_context("styler.format.formatter", None):
312+
assert " 9 " in df.style.to_latex()

0 commit comments

Comments
 (0)