Skip to content

Commit a7669d0

Browse files
committed
Merge pull request #11678 from sinhrks/style_bar
BUG: style.bar results in incorrect gradient using specific browser
2 parents f34bb6d + 965687e commit a7669d0

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

doc/source/whatsnew/v0.18.0.txt

+3
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,7 @@ Bug Fixes
226226
- Bug in ``.loc`` result with duplicated key may have ``Index`` with incorrect dtype (:issue:`11497`)
227227
- Bug in ``pd.rolling_median`` where memory allocation failed even with sufficient memory (:issue:`11696`)
228228

229+
- Bug in ``.style.bar`` may not rendered properly using specific browser (:issue:`11678`)
230+
231+
229232
- Bug in ``df.replace`` while replacing value in mixed dtype ``Dataframe`` (:issue:`11698`)

pandas/core/style.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -656,10 +656,10 @@ def set_properties(self, subset=None, **kwargs):
656656
@staticmethod
657657
def _bar(s, color, width):
658658
normed = width * (s - s.min()) / (s.max() - s.min())
659-
attrs = 'width: 10em; height: 80%;'\
660-
'background: linear-gradient(90deg,'\
661-
'{c} {w}%, transparent 0%)'
662-
return [attrs.format(c=color, w=x) for x in normed]
659+
660+
base = 'width: 10em; height: 80%;'
661+
attrs = base + 'background: linear-gradient(90deg,{c} {w}%, transparent 0%)'
662+
return [attrs.format(c=color, w=x) if x != 0 else base for x in normed]
663663

664664
def bar(self, subset=None, axis=0, color='#d65f5f', width=100):
665665
"""

pandas/tests/test_style.py

+40-6
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,7 @@ def test_bar(self):
198198
df = pd.DataFrame({'A': [0, 1, 2]})
199199
result = df.style.bar()._compute().ctx
200200
expected = {
201-
(0, 0): ['width: 10em', ' height: 80%',
202-
'background: linear-gradient('
203-
'90deg,#d65f5f 0.0%, transparent 0%)'],
201+
(0, 0): ['width: 10em', ' height: 80%'],
204202
(1, 0): ['width: 10em', ' height: 80%',
205203
'background: linear-gradient('
206204
'90deg,#d65f5f 50.0%, transparent 0%)'],
@@ -212,9 +210,7 @@ def test_bar(self):
212210

213211
result = df.style.bar(color='red', width=50)._compute().ctx
214212
expected = {
215-
(0, 0): ['width: 10em', ' height: 80%',
216-
'background: linear-gradient('
217-
'90deg,red 0.0%, transparent 0%)'],
213+
(0, 0): ['width: 10em', ' height: 80%'],
218214
(1, 0): ['width: 10em', ' height: 80%',
219215
'background: linear-gradient('
220216
'90deg,red 25.0%, transparent 0%)'],
@@ -231,6 +227,44 @@ def test_bar(self):
231227
result = df.style.bar(color='red', width=50)._compute().ctx
232228
self.assertEqual(result, expected)
233229

230+
def test_bar_0points(self):
231+
df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
232+
result = df.style.bar()._compute().ctx
233+
expected = {(0, 0): ['width: 10em', ' height: 80%'],
234+
(0, 1): ['width: 10em', ' height: 80%'],
235+
(0, 2): ['width: 10em', ' height: 80%'],
236+
(1, 0): ['width: 10em', ' height: 80%',
237+
'background: linear-gradient(90deg,#d65f5f 50.0%, transparent 0%)'],
238+
(1, 1): ['width: 10em', ' height: 80%',
239+
'background: linear-gradient(90deg,#d65f5f 50.0%, transparent 0%)'],
240+
(1, 2): ['width: 10em', ' height: 80%',
241+
'background: linear-gradient(90deg,#d65f5f 50.0%, transparent 0%)'],
242+
(2, 0): ['width: 10em', ' height: 80%',
243+
'background: linear-gradient(90deg,#d65f5f 100.0%, transparent 0%)'],
244+
(2, 1): ['width: 10em', ' height: 80%',
245+
'background: linear-gradient(90deg,#d65f5f 100.0%, transparent 0%)'],
246+
(2, 2): ['width: 10em', ' height: 80%',
247+
'background: linear-gradient(90deg,#d65f5f 100.0%, transparent 0%)']}
248+
self.assertEqual(result, expected)
249+
250+
result = df.style.bar(axis=1)._compute().ctx
251+
expected = {(0, 0): ['width: 10em', ' height: 80%'],
252+
(0, 1): ['width: 10em', ' height: 80%',
253+
'background: linear-gradient(90deg,#d65f5f 50.0%, transparent 0%)'],
254+
(0, 2): ['width: 10em', ' height: 80%',
255+
'background: linear-gradient(90deg,#d65f5f 100.0%, transparent 0%)'],
256+
(1, 0): ['width: 10em', ' height: 80%'],
257+
(1, 1): ['width: 10em', ' height: 80%',
258+
'background: linear-gradient(90deg,#d65f5f 50.0%, transparent 0%)'],
259+
(1, 2): ['width: 10em', ' height: 80%',
260+
'background: linear-gradient(90deg,#d65f5f 100.0%, transparent 0%)'],
261+
(2, 0): ['width: 10em', ' height: 80%'],
262+
(2, 1): ['width: 10em', ' height: 80%',
263+
'background: linear-gradient(90deg,#d65f5f 50.0%, transparent 0%)'],
264+
(2, 2): ['width: 10em', ' height: 80%',
265+
'background: linear-gradient(90deg,#d65f5f 100.0%, transparent 0%)']}
266+
self.assertEqual(result, expected)
267+
234268
def test_highlight_null(self, null_color='red'):
235269
df = pd.DataFrame({'A': [0, np.nan]})
236270
result = df.style.highlight_null()._compute().ctx

0 commit comments

Comments
 (0)