Skip to content

Commit 7ac2443

Browse files
committed
Changes according to @sinhrks: Raise ValueError instead of warnings when color isn’t a str or 2-tuple/list. Passing “base” from bar().
1 parent e3f714c commit 7ac2443

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

pandas/formats/style.py

+29-17
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
from pandas.types.common import is_float, is_string_like, is_list_like
2121

22-
import warnings
23-
2422
import numpy as np
2523
import pandas as pd
2624
from pandas.compat import range
@@ -859,14 +857,20 @@ def set_properties(self, subset=None, **kwargs):
859857
return self.applymap(f, subset=subset)
860858

861859
@staticmethod
862-
def _bar_left(s, color, width):
860+
def _bar_left(s, color, width, base):
863861
"""
864862
The minimum value is aligned at the left of the cell
865863
.. versionadded:: 0.17.1
866864
867865
Parameters
868866
----------
869867
color: 2-tuple/list, of [``color_negative``, ``color_positive``]
868+
width: float
869+
A number between 0 or 100. The largest value will cover ``width``
870+
percent of the cell's width
871+
base: str
872+
The base css format of the cell, e.g.:
873+
``base = 'width: 10em; height: 80%;'``
870874
871875
Returns
872876
-------
@@ -884,14 +888,20 @@ def _bar_left(s, color, width):
884888
else base for x in normed]
885889

886890
@staticmethod
887-
def _bar_center_zero(s, color, width):
891+
def _bar_center_zero(s, color, width, base):
888892
"""
889893
Creates a bar chart where the zero is centered in the cell
890894
.. versionadded:: 0.19.2
891895
892896
Parameters
893897
----------
894898
color: 2-tuple/list, of [``color_negative``, ``color_positive``]
899+
width: float
900+
A number between 0 or 100. The largest value will cover ``width``
901+
percent of the cell's width
902+
base: str
903+
The base css format of the cell, e.g.:
904+
``base = 'width: 10em; height: 80%;'``
895905
896906
Returns
897907
-------
@@ -904,8 +914,6 @@ def _bar_center_zero(s, color, width):
904914

905915
normed = s * 50 * width / (100 * m)
906916

907-
base = 'width: 10em; height: 80%;'
908-
909917
attrs_neg = (base + 'background: linear-gradient(90deg, transparent 0%'
910918
', transparent {w}%, {c} {w}%, '
911919
'{c} 50%, transparent 50%)')
@@ -919,14 +927,20 @@ def _bar_center_zero(s, color, width):
919927
for x in normed]
920928

921929
@staticmethod
922-
def _bar_center_mid(s, color, width):
930+
def _bar_center_mid(s, color, width, base):
923931
"""
924932
Creates a bar chart where the midpoint is centered in the cell
925933
.. versionadded:: 0.19.2
926934
927935
Parameters
928936
----------
929937
color: 2-tuple/list, of [``color_negative``, ``color_positive``]
938+
width: float
939+
A number between 0 or 100. The largest value will cover ``width``
940+
percent of the cell's width
941+
base: str
942+
The base css format of the cell, e.g.:
943+
``base = 'width: 10em; height: 80%;'``
930944
931945
Returns
932946
-------
@@ -949,8 +963,6 @@ def _bar_center_mid(s, color, width):
949963

950964
normed = zero + slope * s
951965

952-
base = 'width: 10em; height: 80%;'
953-
954966
attrs_neg = (base + 'background: linear-gradient(90deg, transparent 0%'
955967
', transparent {w}%, {c} {w}%, '
956968
'{c} {zero}%, transparent {zero}%)')
@@ -998,27 +1010,27 @@ def bar(self, subset=None, align='left', axis=0,
9981010
subset = _maybe_numeric_slice(self.data, subset)
9991011
subset = _non_reducing_slice(subset)
10001012

1013+
base = 'width: 10em; height: 80%;'
1014+
10011015
if not(is_list_like(color)):
10021016
color = [color, color]
10031017
elif len(color) == 1:
10041018
color = [color[0], color[0]]
10051019
elif len(color) > 2:
10061020
msg = ("Must pass `color` as string or a list-like"
1007-
" of length 2 [ `color_negative`, `color_positive`] "
1008-
"(eg: color=['#d65f5f', '#5fba7d'])\n"
1009-
"Only the first two list elements will be used here.")
1010-
warnings.warn(msg, UserWarning)
1011-
color = [color[0], color[1]]
1021+
" of length 2: [`color_negative`, `color_positive`]\n"
1022+
"(eg: color=['#d65f5f', '#5fba7d'])")
1023+
raise ValueError(msg)
10121024

10131025
if align == 'left':
10141026
self.apply(self._bar_left, subset=subset, axis=axis, color=color,
1015-
width=width)
1027+
width=width, base=base)
10161028
elif align == 'zero':
10171029
self.apply(self._bar_center_zero, subset=subset, axis=axis,
1018-
color=color, width=width)
1030+
color=color, width=width, base=base)
10191031
elif align == 'mid':
10201032
self.apply(self._bar_center_mid, subset=subset, axis=axis,
1021-
color=color, width=width)
1033+
color=color, width=width, base=base)
10221034
return self
10231035

10241036
def highlight_max(self, subset=None, color='yellow', axis=0):

0 commit comments

Comments
 (0)