diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index f51a1f5d9809d..8eef89f3cb28a 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -108,7 +108,9 @@ def __init__( self.cell_context: DefaultDict[tuple[int, int], str] = defaultdict(str) self._todo: list[tuple[Callable, tuple, dict]] = [] self.tooltips: Tooltips | None = None - precision = precision or get_option("styler.format.precision") + precision = ( + get_option("styler.format.precision") if precision is None else precision + ) self._display_funcs: DefaultDict[ # maps (row, col) -> formatting function tuple[int, int], Callable[[Any], str] ] = defaultdict(lambda: partial(_default_formatter, precision=precision)) @@ -1033,7 +1035,9 @@ def _maybe_wrap_formatter( elif callable(formatter): func_0 = formatter elif formatter is None: - precision = precision or get_option("styler.format.precision") + precision = ( + get_option("styler.format.precision") if precision is None else precision + ) func_0 = partial( _default_formatter, precision=precision, thousands=(thousands is not None) ) diff --git a/pandas/tests/io/formats/style/test_format.py b/pandas/tests/io/formats/style/test_format.py index 58f18a6959efa..532aeb26f6d21 100644 --- a/pandas/tests/io/formats/style/test_format.py +++ b/pandas/tests/io/formats/style/test_format.py @@ -298,3 +298,10 @@ def test_format_options(): with option_context("styler.format.formatter", {"int": "{:,.2f}"}): ctx_with_op = df.style._translate(True, True) assert ctx_with_op["body"][0][1]["display_value"] == "2,000.00" + + +def test_precision_zero(df): + styler = Styler(df, precision=0) + ctx = styler._translate(True, True) + assert ctx["body"][0][2]["display_value"] == "-1" + assert ctx["body"][1][2]["display_value"] == "-1"