Skip to content

Commit 703b1ef

Browse files
authored
BUG: styler.format options and validator tests (#43341)
1 parent f6a170e commit 703b1ef

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

pandas/core/config_init.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
1111
"""
1212
import os
13+
from typing import Callable
1314
import warnings
1415

1516
import pandas._config.config as cf
@@ -879,7 +880,7 @@ def register_converter_cb(key):
879880
"format.formatter",
880881
None,
881882
styler_formatter,
882-
validator=is_instance_factory([type(None), dict, callable, str]),
883+
validator=is_instance_factory([type(None), dict, Callable, str]),
883884
)
884885

885886
cf.register_option("html.mathjax", True, styler_mathjax, validator=is_bool)

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

+22
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,28 @@ def test_precision_zero(df):
308308
assert ctx["body"][1][2]["display_value"] == "-1"
309309

310310

311+
@pytest.mark.parametrize(
312+
"formatter, exp",
313+
[
314+
(lambda x: f"{x:.3f}", "9.000"),
315+
("{:.2f}", "9.00"),
316+
({0: "{:.1f}"}, "9.0"),
317+
(None, "9"),
318+
],
319+
)
320+
def test_formatter_options_validator(formatter, exp):
321+
df = DataFrame([[9]])
322+
with option_context("styler.format.formatter", formatter):
323+
assert f" {exp} " in df.style.to_latex()
324+
325+
326+
def test_formatter_options_raises():
327+
msg = "Value must be an instance of"
328+
with pytest.raises(ValueError, match=msg):
329+
with option_context("styler.format.formatter", ["bad", "type"]):
330+
DataFrame().style.to_latex()
331+
332+
311333
def test_1level_multiindex():
312334
# GH 43383
313335
midx = MultiIndex.from_product([[1, 2]], names=[""])

0 commit comments

Comments
 (0)