|
| 1 | +import io |
| 2 | + |
1 | 3 | import numpy as np
|
2 | 4 | import pytest
|
3 | 5 |
|
4 |
| -from pandas import DataFrame |
| 6 | +from pandas import ( |
| 7 | + NA, |
| 8 | + DataFrame, |
| 9 | + read_csv, |
| 10 | +) |
5 | 11 |
|
6 | 12 | pytest.importorskip("jinja2")
|
7 | 13 |
|
@@ -305,3 +311,48 @@ def test_bar_value_error_raises():
|
305 | 311 | msg = r"`height` must be a value in \[0, 100\]"
|
306 | 312 | with pytest.raises(ValueError, match=msg):
|
307 | 313 | df.style.bar(height=200).to_html()
|
| 314 | + |
| 315 | + |
| 316 | +def test_bar_color_and_cmap_error_raises(): |
| 317 | + df = DataFrame({"A": [1, 2, 3, 4]}) |
| 318 | + msg = "`color` and `cmap` cannot both be given" |
| 319 | + # Test that providing both color and cmap raises a ValueError |
| 320 | + with pytest.raises(ValueError, match=msg): |
| 321 | + df.style.bar(color="#d65f5f", cmap="viridis").to_html() |
| 322 | + |
| 323 | + |
| 324 | +def test_bar_invalid_color_type_error_raises(): |
| 325 | + df = DataFrame({"A": [1, 2, 3, 4]}) |
| 326 | + msg = ( |
| 327 | + r"`color` must be string or list or tuple of 2 strings," |
| 328 | + r"\(eg: color=\['#d65f5f', '#5fba7d'\]\)" |
| 329 | + ) |
| 330 | + # Test that providing an invalid color type raises a ValueError |
| 331 | + with pytest.raises(ValueError, match=msg): |
| 332 | + df.style.bar(color=123).to_html() |
| 333 | + |
| 334 | + # Test that providing a color list with more than two elements raises a ValueError |
| 335 | + with pytest.raises(ValueError, match=msg): |
| 336 | + df.style.bar(color=["#d65f5f", "#5fba7d", "#abcdef"]).to_html() |
| 337 | + |
| 338 | + |
| 339 | +def test_styler_bar_with_NA_values(): |
| 340 | + df1 = DataFrame({"A": [1, 2, NA, 4]}) |
| 341 | + df2 = DataFrame([[NA, NA], [NA, NA]]) |
| 342 | + expected_substring = "style type=" |
| 343 | + html_output1 = df1.style.bar(subset="A").to_html() |
| 344 | + html_output2 = df2.style.bar(align="left", axis=None).to_html() |
| 345 | + assert expected_substring in html_output1 |
| 346 | + assert expected_substring in html_output2 |
| 347 | + |
| 348 | + |
| 349 | +def test_style_bar_with_pyarrow_NA_values(): |
| 350 | + data = """name,age,test1,test2,teacher |
| 351 | + Adam,15,95.0,80,Ashby |
| 352 | + Bob,16,81.0,82,Ashby |
| 353 | + Dave,16,89.0,84,Jones |
| 354 | + Fred,15,,88,Jones""" |
| 355 | + df = read_csv(io.StringIO(data), dtype_backend="pyarrow") |
| 356 | + expected_substring = "style type=" |
| 357 | + html_output = df.style.bar(subset="test1").to_html() |
| 358 | + assert expected_substring in html_output |
0 commit comments