From df9b9a9cb9c33e36b0781accc7627f70ef203c2c Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Mon, 12 Jul 2021 19:20:11 +0200 Subject: [PATCH 1/4] styler.bar validation on arg width --- pandas/io/formats/style.py | 3 +++ pandas/tests/io/formats/style/test_bar.py | 9 +++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 3465d353cc2ca..e9e7d81864a2b 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -2121,6 +2121,9 @@ def bar( "(eg: color=['#d65f5f', '#5fba7d'])" ) + if not (0 <= width <= 100): + raise ValueError(f"`width` must be a value in [0, 100], got {width}") + if subset is None: subset = self.data.select_dtypes(include=np.number).columns diff --git a/pandas/tests/io/formats/style/test_bar.py b/pandas/tests/io/formats/style/test_bar.py index 18a0c39a5cbc0..5cc50947be564 100644 --- a/pandas/tests/io/formats/style/test_bar.py +++ b/pandas/tests/io/formats/style/test_bar.py @@ -319,8 +319,13 @@ def test_colors_mixed(align, exp): assert result == {(0, 0): exp[0], (1, 0): exp[1]} -def test_bar_bad_align_raises(): +def test_bar_value_error_raises(): df = DataFrame({"A": [-100, -60, -30, -20]}) + msg = "`align` should be in {'left', 'right', 'mid', 'mean', 'zero'} or" with pytest.raises(ValueError, match=msg): - df.style.bar(align="poorly", color=["#d65f5f", "#5fba7d"]).render() + df.style.bar(align="poorly", color=["#d65f5f", "#5fba7d"]).to_html() + + msg = r"`width` must be a value in \[0, 100\]" + with pytest.raises(ValueError, match=msg): + df.style.bar(width=200).to_html() From a8677da759f49d01913b900fa75cd9c681aee83b Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Mon, 12 Jul 2021 19:23:42 +0200 Subject: [PATCH 2/4] styler.bar validation on arg width --- doc/source/whatsnew/v1.4.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 26dac44f0d15f..38a32f7910fb8 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -32,6 +32,7 @@ Other enhancements - Add support for assigning values to ``by`` argument in :meth:`DataFrame.plot.hist` and :meth:`DataFrame.plot.box` (:issue:`15079`) - :meth:`Series.sample`, :meth:`DataFrame.sample`, and :meth:`.GroupBy.sample` now accept a ``np.random.Generator`` as input to ``random_state``. A generator will be more performant, especially with ``replace=False`` (:issue:`38100`) - Additional options added to :meth:`.Styler.bar` to control alignment and display (:issue:`26070`) +- :meth:`Styler.bar` now validates the input argument ``width`` (:issue:`42511`) - :meth:`Series.ewm`, :meth:`DataFrame.ewm`, now support a ``method`` argument with a ``'table'`` option that performs the windowing operation over an entire :class:`DataFrame`. See :ref:`Window Overview ` for performance and functional benefits (:issue:`42273`) - From a5d3d3dab27b42ba7d5feec951909f7e08997231 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Tue, 13 Jul 2021 15:36:47 +0200 Subject: [PATCH 3/4] height validation --- pandas/io/formats/style.py | 2 ++ pandas/tests/io/formats/style/test_bar.py | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index cea14b8a3931b..cb56ea33acad8 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -2131,6 +2131,8 @@ def bar( if not (0 <= width <= 100): raise ValueError(f"`width` must be a value in [0, 100], got {width}") + elif not (0 <= height <= 100): + raise ValueError(f"`height` must be a value in [0, 100], got {height}") if subset is None: subset = self.data.select_dtypes(include=np.number).columns diff --git a/pandas/tests/io/formats/style/test_bar.py b/pandas/tests/io/formats/style/test_bar.py index 86987339a454f..19884aaac86a7 100644 --- a/pandas/tests/io/formats/style/test_bar.py +++ b/pandas/tests/io/formats/style/test_bar.py @@ -301,3 +301,7 @@ def test_bar_value_error_raises(): msg = r"`width` must be a value in \[0, 100\]" with pytest.raises(ValueError, match=msg): df.style.bar(width=200).to_html() + + msg = r"`height` must be a value in \[0, 100\]" + with pytest.raises(ValueError, match=msg): + df.style.bar(height=200).to_html() From bae82c671fcbff74d480b38bae3db1ab97eaa8a3 Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Tue, 13 Jul 2021 15:37:46 +0200 Subject: [PATCH 4/4] height validation --- doc/source/whatsnew/v1.4.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 375bb2b5d0449..a63bd2fe749f5 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -32,7 +32,7 @@ Other enhancements - Add support for assigning values to ``by`` argument in :meth:`DataFrame.plot.hist` and :meth:`DataFrame.plot.box` (:issue:`15079`) - :meth:`Series.sample`, :meth:`DataFrame.sample`, and :meth:`.GroupBy.sample` now accept a ``np.random.Generator`` as input to ``random_state``. A generator will be more performant, especially with ``replace=False`` (:issue:`38100`) - Additional options added to :meth:`.Styler.bar` to control alignment and display, with keyword only arguments (:issue:`26070`, :issue:`36419`) -- :meth:`Styler.bar` now validates the input argument ``width`` (:issue:`42511`) +- :meth:`Styler.bar` now validates the input argument ``width`` and ``height`` (:issue:`42511`) - :meth:`Series.ewm`, :meth:`DataFrame.ewm`, now support a ``method`` argument with a ``'table'`` option that performs the windowing operation over an entire :class:`DataFrame`. See :ref:`Window Overview ` for performance and functional benefits (:issue:`42273`) -