Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <window.overview>` for performance and functional benefits (:issue:`42273`)
-

Expand Down
3 changes: 3 additions & 0 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -2121,6 +2121,9 @@ def bar(
"(eg: color=['#d65f5f', '#5fba7d'])"
)

if not (0 <= width <= 100):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

height? other ones?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will add height after other PR which adds feature

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

Expand Down
9 changes: 7 additions & 2 deletions pandas/tests/io/formats/style/test_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()