From b4dbbcd7f46dbb1dd2c346df81d3396d8c1c609f Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Wed, 1 Sep 2021 15:41:45 +0200 Subject: [PATCH 1/2] refactor to account for precision=0 --- pandas/io/formats/style_render.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index f51a1f5d9809d..86b913d52b8fe 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)) From c80c6c150b719e0f1eb6dfab47e299facdefa926 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Wed, 1 Sep 2021 15:47:38 +0200 Subject: [PATCH 2/2] refactor to account for precision=0 --- pandas/io/formats/style_render.py | 4 +++- pandas/tests/io/formats/style/test_format.py | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index 86b913d52b8fe..8eef89f3cb28a 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -1035,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"