Skip to content

Commit

Permalink
TST: Improved test coverage for Styler.bar error conditions (#56341)
Browse files Browse the repository at this point in the history
* Improved test coverage for Styler.bar error conditions

* Fixed the code style issue causing test failure

* Fixed a Styler.bar bug of missing value

* Adopted another tricky way to fix the bug

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Added a test for Styler.bar with pyarrow

* Updated with code style

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
ccccjone and pre-commit-ci[bot] authored Dec 11, 2023
1 parent b0ffccd commit d352d5a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
5 changes: 3 additions & 2 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -4082,8 +4082,9 @@ def css_calc(x, left: float, right: float, align: str, color: str | list | tuple
return ret

values = data.to_numpy()
left = np.nanmin(values) if vmin is None else vmin
right = np.nanmax(values) if vmax is None else vmax
# A tricky way to address the issue where np.nanmin/np.nanmax fail to handle pd.NA.
left = np.nanmin(data.min(skipna=True)) if vmin is None else vmin
right = np.nanmax(data.max(skipna=True)) if vmax is None else vmax
z: float = 0 # adjustment to translate data

if align == "mid":
Expand Down
53 changes: 52 additions & 1 deletion pandas/tests/io/formats/style/test_bar.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import io

import numpy as np
import pytest

from pandas import DataFrame
from pandas import (
NA,
DataFrame,
read_csv,
)

pytest.importorskip("jinja2")

Expand Down Expand Up @@ -305,3 +311,48 @@ def test_bar_value_error_raises():
msg = r"`height` must be a value in \[0, 100\]"
with pytest.raises(ValueError, match=msg):
df.style.bar(height=200).to_html()


def test_bar_color_and_cmap_error_raises():
df = DataFrame({"A": [1, 2, 3, 4]})
msg = "`color` and `cmap` cannot both be given"
# Test that providing both color and cmap raises a ValueError
with pytest.raises(ValueError, match=msg):
df.style.bar(color="#d65f5f", cmap="viridis").to_html()


def test_bar_invalid_color_type_error_raises():
df = DataFrame({"A": [1, 2, 3, 4]})
msg = (
r"`color` must be string or list or tuple of 2 strings,"
r"\(eg: color=\['#d65f5f', '#5fba7d'\]\)"
)
# Test that providing an invalid color type raises a ValueError
with pytest.raises(ValueError, match=msg):
df.style.bar(color=123).to_html()

# Test that providing a color list with more than two elements raises a ValueError
with pytest.raises(ValueError, match=msg):
df.style.bar(color=["#d65f5f", "#5fba7d", "#abcdef"]).to_html()


def test_styler_bar_with_NA_values():
df1 = DataFrame({"A": [1, 2, NA, 4]})
df2 = DataFrame([[NA, NA], [NA, NA]])
expected_substring = "style type="
html_output1 = df1.style.bar(subset="A").to_html()
html_output2 = df2.style.bar(align="left", axis=None).to_html()
assert expected_substring in html_output1
assert expected_substring in html_output2


def test_style_bar_with_pyarrow_NA_values():
data = """name,age,test1,test2,teacher
Adam,15,95.0,80,Ashby
Bob,16,81.0,82,Ashby
Dave,16,89.0,84,Jones
Fred,15,,88,Jones"""
df = read_csv(io.StringIO(data), dtype_backend="pyarrow")
expected_substring = "style type="
html_output = df.style.bar(subset="test1").to_html()
assert expected_substring in html_output

0 comments on commit d352d5a

Please sign in to comment.