Skip to content

Commit fe934d8

Browse files
authored
ENH: validate the argument width on Styler.bar (#42511)
1 parent ecfbb26 commit fe934d8

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Other enhancements
3232
- Add support for assigning values to ``by`` argument in :meth:`DataFrame.plot.hist` and :meth:`DataFrame.plot.box` (:issue:`15079`)
3333
- :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`)
3434
- Additional options added to :meth:`.Styler.bar` to control alignment and display, with keyword only arguments (:issue:`26070`, :issue:`36419`)
35+
- :meth:`Styler.bar` now validates the input argument ``width`` and ``height`` (:issue:`42511`)
3536
- :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 <window.overview>` for performance and functional benefits (:issue:`42273`)
3637
-
3738

pandas/io/formats/style.py

+5
Original file line numberDiff line numberDiff line change
@@ -2129,6 +2129,11 @@ def bar(
21292129
"(eg: color=['#d65f5f', '#5fba7d'])"
21302130
)
21312131

2132+
if not (0 <= width <= 100):
2133+
raise ValueError(f"`width` must be a value in [0, 100], got {width}")
2134+
elif not (0 <= height <= 100):
2135+
raise ValueError(f"`height` must be a value in [0, 100], got {height}")
2136+
21322137
if subset is None:
21332138
subset = self.data.select_dtypes(include=np.number).columns
21342139

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,17 @@ def test_bar_align_height():
291291
assert result == expected
292292

293293

294-
def test_bar_bad_align_raises():
294+
def test_bar_value_error_raises():
295295
df = DataFrame({"A": [-100, -60, -30, -20]})
296+
296297
msg = "`align` should be in {'left', 'right', 'mid', 'mean', 'zero'} or"
297298
with pytest.raises(ValueError, match=msg):
298-
df.style.bar(align="poorly", color=["#d65f5f", "#5fba7d"]).render()
299+
df.style.bar(align="poorly", color=["#d65f5f", "#5fba7d"]).to_html()
300+
301+
msg = r"`width` must be a value in \[0, 100\]"
302+
with pytest.raises(ValueError, match=msg):
303+
df.style.bar(width=200).to_html()
304+
305+
msg = r"`height` must be a value in \[0, 100\]"
306+
with pytest.raises(ValueError, match=msg):
307+
df.style.bar(height=200).to_html()

0 commit comments

Comments
 (0)