Skip to content

Commit e18cc0d

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 81c9e38 commit e18cc0d

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
@@ -848,14 +846,20 @@ def set_properties(self, subset=None, **kwargs):
848846
return self.applymap(f, subset=subset)
849847

850848
@staticmethod
851-
def _bar_left(s, color, width):
849+
def _bar_left(s, color, width, base):
852850
"""
853851
The minimum value is aligned at the left of the cell
854852
.. versionadded:: 0.17.1
855853
856854
Parameters
857855
----------
858856
color: 2-tuple/list, of [``color_negative``, ``color_positive``]
857+
width: float
858+
A number between 0 or 100. The largest value will cover ``width``
859+
percent of the cell's width
860+
base: str
861+
The base css format of the cell, e.g.:
862+
``base = 'width: 10em; height: 80%;'``
859863
860864
Returns
861865
-------
@@ -873,14 +877,20 @@ def _bar_left(s, color, width):
873877
else base for x in normed]
874878

875879
@staticmethod
876-
def _bar_center_zero(s, color, width):
880+
def _bar_center_zero(s, color, width, base):
877881
"""
878882
Creates a bar chart where the zero is centered in the cell
879883
.. versionadded:: 0.19.2
880884
881885
Parameters
882886
----------
883887
color: 2-tuple/list, of [``color_negative``, ``color_positive``]
888+
width: float
889+
A number between 0 or 100. The largest value will cover ``width``
890+
percent of the cell's width
891+
base: str
892+
The base css format of the cell, e.g.:
893+
``base = 'width: 10em; height: 80%;'``
884894
885895
Returns
886896
-------
@@ -893,8 +903,6 @@ def _bar_center_zero(s, color, width):
893903

894904
normed = s * 50 * width / (100 * m)
895905

896-
base = 'width: 10em; height: 80%;'
897-
898906
attrs_neg = (base + 'background: linear-gradient(90deg, transparent 0%'
899907
', transparent {w}%, {c} {w}%, '
900908
'{c} 50%, transparent 50%)')
@@ -908,14 +916,20 @@ def _bar_center_zero(s, color, width):
908916
for x in normed]
909917

910918
@staticmethod
911-
def _bar_center_mid(s, color, width):
919+
def _bar_center_mid(s, color, width, base):
912920
"""
913921
Creates a bar chart where the midpoint is centered in the cell
914922
.. versionadded:: 0.19.2
915923
916924
Parameters
917925
----------
918926
color: 2-tuple/list, of [``color_negative``, ``color_positive``]
927+
width: float
928+
A number between 0 or 100. The largest value will cover ``width``
929+
percent of the cell's width
930+
base: str
931+
The base css format of the cell, e.g.:
932+
``base = 'width: 10em; height: 80%;'``
919933
920934
Returns
921935
-------
@@ -938,8 +952,6 @@ def _bar_center_mid(s, color, width):
938952

939953
normed = zero + slope * s
940954

941-
base = 'width: 10em; height: 80%;'
942-
943955
attrs_neg = (base + 'background: linear-gradient(90deg, transparent 0%'
944956
', transparent {w}%, {c} {w}%, '
945957
'{c} {zero}%, transparent {zero}%)')
@@ -987,27 +999,27 @@ def bar(self, subset=None, align='left', axis=0,
987999
subset = _maybe_numeric_slice(self.data, subset)
9881000
subset = _non_reducing_slice(subset)
9891001

1002+
base = 'width: 10em; height: 80%;'
1003+
9901004
if not(is_list_like(color)):
9911005
color = [color, color]
9921006
elif len(color) == 1:
9931007
color = [color[0], color[0]]
9941008
elif len(color) > 2:
9951009
msg = ("Must pass `color` as string or a list-like"
996-
" of length 2 [ `color_negative`, `color_positive`] "
997-
"(eg: color=['#d65f5f', '#5fba7d'])\n"
998-
"Only the first two list elements will be used here.")
999-
warnings.warn(msg, UserWarning)
1000-
color = [color[0], color[1]]
1010+
" of length 2: [`color_negative`, `color_positive`]\n"
1011+
"(eg: color=['#d65f5f', '#5fba7d'])")
1012+
raise ValueError(msg)
10011013

10021014
if align == 'left':
10031015
self.apply(self._bar_left, subset=subset, axis=axis, color=color,
1004-
width=width)
1016+
width=width, base=base)
10051017
elif align == 'zero':
10061018
self.apply(self._bar_center_zero, subset=subset, axis=axis,
1007-
color=color, width=width)
1019+
color=color, width=width, base=base)
10081020
elif align == 'mid':
10091021
self.apply(self._bar_center_mid, subset=subset, axis=axis,
1010-
color=color, width=width)
1022+
color=color, width=width, base=base)
10111023
return self
10121024

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

0 commit comments

Comments
 (0)