Skip to content

Commit b22f639

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 3046626 commit b22f639

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
@@ -853,14 +851,20 @@ def set_properties(self, subset=None, **kwargs):
853851
return self.applymap(f, subset=subset)
854852

855853
@staticmethod
856-
def _bar_left(s, color, width):
854+
def _bar_left(s, color, width, base):
857855
"""
858856
The minimum value is aligned at the left of the cell
859857
.. versionadded:: 0.17.1
860858
861859
Parameters
862860
----------
863861
color: 2-tuple/list, of [``color_negative``, ``color_positive``]
862+
width: float
863+
A number between 0 or 100. The largest value will cover ``width``
864+
percent of the cell's width
865+
base: str
866+
The base css format of the cell, e.g.:
867+
``base = 'width: 10em; height: 80%;'``
864868
865869
Returns
866870
-------
@@ -878,14 +882,20 @@ def _bar_left(s, color, width):
878882
else base for x in normed]
879883

880884
@staticmethod
881-
def _bar_center_zero(s, color, width):
885+
def _bar_center_zero(s, color, width, base):
882886
"""
883887
Creates a bar chart where the zero is centered in the cell
884888
.. versionadded:: 0.19.2
885889
886890
Parameters
887891
----------
888892
color: 2-tuple/list, of [``color_negative``, ``color_positive``]
893+
width: float
894+
A number between 0 or 100. The largest value will cover ``width``
895+
percent of the cell's width
896+
base: str
897+
The base css format of the cell, e.g.:
898+
``base = 'width: 10em; height: 80%;'``
889899
890900
Returns
891901
-------
@@ -898,8 +908,6 @@ def _bar_center_zero(s, color, width):
898908

899909
normed = s * 50 * width / (100 * m)
900910

901-
base = 'width: 10em; height: 80%;'
902-
903911
attrs_neg = (base + 'background: linear-gradient(90deg, transparent 0%'
904912
', transparent {w}%, {c} {w}%, '
905913
'{c} 50%, transparent 50%)')
@@ -913,14 +921,20 @@ def _bar_center_zero(s, color, width):
913921
for x in normed]
914922

915923
@staticmethod
916-
def _bar_center_mid(s, color, width):
924+
def _bar_center_mid(s, color, width, base):
917925
"""
918926
Creates a bar chart where the midpoint is centered in the cell
919927
.. versionadded:: 0.19.2
920928
921929
Parameters
922930
----------
923931
color: 2-tuple/list, of [``color_negative``, ``color_positive``]
932+
width: float
933+
A number between 0 or 100. The largest value will cover ``width``
934+
percent of the cell's width
935+
base: str
936+
The base css format of the cell, e.g.:
937+
``base = 'width: 10em; height: 80%;'``
924938
925939
Returns
926940
-------
@@ -943,8 +957,6 @@ def _bar_center_mid(s, color, width):
943957

944958
normed = zero + slope * s
945959

946-
base = 'width: 10em; height: 80%;'
947-
948960
attrs_neg = (base + 'background: linear-gradient(90deg, transparent 0%'
949961
', transparent {w}%, {c} {w}%, '
950962
'{c} {zero}%, transparent {zero}%)')
@@ -992,27 +1004,27 @@ def bar(self, subset=None, align='left', axis=0,
9921004
subset = _maybe_numeric_slice(self.data, subset)
9931005
subset = _non_reducing_slice(subset)
9941006

1007+
base = 'width: 10em; height: 80%;'
1008+
9951009
if not(is_list_like(color)):
9961010
color = [color, color]
9971011
elif len(color) == 1:
9981012
color = [color[0], color[0]]
9991013
elif len(color) > 2:
10001014
msg = ("Must pass `color` as string or a list-like"
1001-
" of length 2 [ `color_negative`, `color_positive`] "
1002-
"(eg: color=['#d65f5f', '#5fba7d'])\n"
1003-
"Only the first two list elements will be used here.")
1004-
warnings.warn(msg, UserWarning)
1005-
color = [color[0], color[1]]
1015+
" of length 2: [`color_negative`, `color_positive`]\n"
1016+
"(eg: color=['#d65f5f', '#5fba7d'])")
1017+
raise ValueError(msg)
10061018

10071019
if align == 'left':
10081020
self.apply(self._bar_left, subset=subset, axis=axis, color=color,
1009-
width=width)
1021+
width=width, base=base)
10101022
elif align == 'zero':
10111023
self.apply(self._bar_center_zero, subset=subset, axis=axis,
1012-
color=color, width=width)
1024+
color=color, width=width, base=base)
10131025
elif align == 'mid':
10141026
self.apply(self._bar_center_mid, subset=subset, axis=axis,
1015-
color=color, width=width)
1027+
color=color, width=width, base=base)
10161028
return self
10171029

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

0 commit comments

Comments
 (0)