Skip to content

Commit bb6de10

Browse files
authored
test_bar.py cleanup 3/3: NaN cases (#42510)
Co-authored-by: JHM Darbyshire (iMac) <[email protected]>
1 parent 4c90215 commit bb6de10

File tree

1 file changed

+8
-52
lines changed

1 file changed

+8
-52
lines changed

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

+8-52
Original file line numberDiff line numberDiff line change
@@ -51,55 +51,6 @@ def df_mix():
5151
return DataFrame([[-3], [1], [2]])
5252

5353

54-
class TestStylerBarAlign:
55-
def test_bar_align_mid_nans(self):
56-
df = DataFrame({"A": [1, None], "B": [-1, 3]})
57-
result = df.style.bar(align="mid", axis=None)._compute().ctx
58-
expected = {
59-
(0, 0): bar_grad(
60-
" transparent 25.0%",
61-
" #d65f5f 25.0%",
62-
" #d65f5f 50.0%",
63-
" transparent 50.0%",
64-
),
65-
(0, 1): bar_grad(" #d65f5f 25.0%", " transparent 25.0%"),
66-
(1, 0): bar_grad(),
67-
(1, 1): bar_grad(
68-
" transparent 25.0%",
69-
" #d65f5f 25.0%",
70-
" #d65f5f 100.0%",
71-
" transparent 100.0%",
72-
),
73-
}
74-
assert result == expected
75-
76-
def test_bar_align_zero_nans(self):
77-
df = DataFrame({"A": [1, None], "B": [-1, 2]})
78-
result = df.style.bar(align="zero", axis=None)._compute().ctx
79-
expected = {
80-
(0, 0): bar_grad(
81-
" transparent 50.0%",
82-
" #d65f5f 50.0%",
83-
" #d65f5f 75.0%",
84-
" transparent 75.0%",
85-
),
86-
(0, 1): bar_grad(
87-
" transparent 25.0%",
88-
" #d65f5f 25.0%",
89-
" #d65f5f 50.0%",
90-
" transparent 50.0%",
91-
),
92-
(1, 0): bar_grad(),
93-
(1, 1): bar_grad(
94-
" transparent 50.0%",
95-
" #d65f5f 50.0%",
96-
" #d65f5f 100.0%",
97-
" transparent 100.0%",
98-
),
99-
}
100-
assert result == expected
101-
102-
10354
@pytest.mark.parametrize(
10455
"align, exp",
10556
[
@@ -147,13 +98,18 @@ def test_align_negative_cases(df_neg, align, exp):
14798
("zero", [bar_to(50), bar_from_to(50, 66.66), bar_from_to(50, 83.33)]),
14899
("mean", [bar_to(50), bar_from_to(50, 66.66), bar_from_to(50, 83.33)]),
149100
(-0.0, [bar_to(50), bar_from_to(50, 66.66), bar_from_to(50, 83.33)]),
150-
(np.median, [bar_to(50), no_bar(), bar_from_to(50, 62.5)]),
101+
(np.nanmedian, [bar_to(50), no_bar(), bar_from_to(50, 62.5)]),
151102
],
152103
)
153-
def test_align_mixed_cases(df_mix, align, exp):
104+
@pytest.mark.parametrize("nans", [True, False])
105+
def test_align_mixed_cases(df_mix, align, exp, nans):
154106
# test different align cases for mixed positive and negative values
155-
result = df_mix.style.bar(align=align)._compute().ctx
107+
# also test no impact of NaNs and no_bar
156108
expected = {(0, 0): exp[0], (1, 0): exp[1], (2, 0): exp[2]}
109+
if nans:
110+
df_mix.loc[3, :] = np.nan
111+
expected.update({(3, 0): no_bar()})
112+
result = df_mix.style.bar(align=align)._compute().ctx
157113
assert result == expected
158114

159115

0 commit comments

Comments
 (0)