def test_maybe_numeric_slice(self): df = DataFrame({'A': [1, 2], 'B': ['c', 'd'], 'C': [True, False]}) result = _maybe_numeric_slice(df, slice_=None) expected = pd.IndexSlice[:, ['A']] assert result == expected result = _maybe_numeric_slice(df, None, include_bool=True) expected = pd.IndexSlice[:, ['A', 'C']] result = _maybe_numeric_slice(df, [1]) expected = [1] assert result == expected
def test_maybe_numeric_slice(self): df = DataFrame({"A": [1, 2], "B": ["c", "d"], "C": [True, False]}) result = _maybe_numeric_slice(df, slice_=None) expected = pd.IndexSlice[:, ["A"]] assert result == expected result = _maybe_numeric_slice(df, None, include_bool=True) expected = pd.IndexSlice[:, ["A", "C"]] result = _maybe_numeric_slice(df, [1]) expected = [1] assert result == expected
def test_maybe_numeric_slice(self): df = pd.DataFrame({'A': [1, 2], 'B': ['c', 'd'], 'C': [True, False]}) result = _maybe_numeric_slice(df, slice_=None) expected = pd.IndexSlice[:, ['A']] assert result == expected result = _maybe_numeric_slice(df, None, include_bool=True) expected = pd.IndexSlice[:, ['A', 'C']] result = _maybe_numeric_slice(df, [1]) expected = [1] assert result == expected
def background_gradient(self, cmap='PuBu', low=0, high=0, axis=0, subset=None): """ Color the background in a gradient according to the data in each column (optionally row). Requires matplotlib. Parameters ---------- cmap: str or colormap matplotlib colormap low, high: float compress the range by these values. axis: int or str 1 or 'columns' for columnwise, 0 or 'index' for rowwise subset: IndexSlice a valid slice for ``data`` to limit the style application to Returns ------- self : Styler Notes ----- Tune ``low`` and ``high`` to keep the text legible by not using the entire range of the color map. These extend the range of the data by ``low * (x.max() - x.min())`` and ``high * (x.max() - x.min())`` before normalizing. """ subset = _maybe_numeric_slice(self.data, subset) subset = _non_reducing_slice(subset) self.apply(self._background_gradient, cmap=cmap, subset=subset, axis=axis, low=low, high=high) return self
def bar(self, subset=None, axis=0, color='#d65f5f', width=100): """ Color the background ``color`` proptional to the values in each column. Excludes non-numeric data by default. .. versionadded:: 0.17.1 Parameters ---------- subset: IndexSlice, default None a valid slice for ``data`` to limit the style application to axis: int color: str width: float A number between 0 or 100. The largest value will cover ``width`` percent of the cell's width Returns ------- self : Styler """ subset = _maybe_numeric_slice(self.data, subset) subset = _non_reducing_slice(subset) self.apply(self._bar, subset=subset, axis=axis, color=color, width=width) return self
def background_gradient(self, cmap='PuBu', low=0, high=0, axis=0, subset=None, text_color_threshold=0.408): """ Color the background in a gradient according to the data in each column (optionally row). Requires matplotlib. Parameters ---------- cmap : str or colormap matplotlib colormap low, high : float compress the range by these values. axis : {0 or 'index', 1 or 'columns', None}, default 0 apply to each column (``axis=0`` or ``'index'``), to each row (``axis=1`` or ``'columns'``), or to the entire DataFrame at once with ``axis=None``. subset : IndexSlice a valid slice for ``data`` to limit the style application to. text_color_threshold : float or int luminance threshold for determining text color. Facilitates text visibility across varying background colors. From 0 to 1. 0 = all text is dark colored, 1 = all text is light colored. .. versionadded:: 0.24.0 Returns ------- self : Styler Raises ------ ValueError If ``text_color_threshold`` is not a value from 0 to 1. Notes ----- Set ``text_color_threshold`` or tune ``low`` and ``high`` to keep the text legible by not using the entire range of the color map. The range of the data is extended by ``low * (x.max() - x.min())`` and ``high * (x.max() - x.min())`` before normalizing. """ subset = _maybe_numeric_slice(self.data, subset) subset = _non_reducing_slice(subset) self.apply(self._background_gradient, cmap=cmap, subset=subset, axis=axis, low=low, high=high, text_color_threshold=text_color_threshold) return self
def _highlight_handler(self, subset=None, color='yellow', axis=None, max_=True): subset = _non_reducing_slice(_maybe_numeric_slice(self.data, subset)) self.apply(self._highlight_extrema, color=color, axis=axis, subset=subset, max_=max_) return self
def bar(self, subset=None, axis=0, color='#d65f5f', width=100, align='left'): """ Color the background ``color`` proportional to the values in each column. Excludes non-numeric data by default. Parameters ---------- subset: IndexSlice, default None a valid slice for ``data`` to limit the style application to axis: int color: str or 2-tuple/list If a str is passed, the color is the same for both negative and positive numbers. If 2-tuple/list is used, the first element is the color_negative and the second is the color_positive (eg: ['#d65f5f', '#5fba7d']) width: float A number between 0 or 100. The largest value will cover ``width`` percent of the cell's width align : {'left', 'zero',' mid'}, default 'left' - 'left' : the min value starts at the left of the cell - 'zero' : a value of zero is located at the center of the cell - 'mid' : the center of the cell is at (max-min)/2, or if values are all negative (positive) the zero is aligned at the right (left) of the cell .. versionadded:: 0.20.0 Returns ------- self : Styler """ subset = _maybe_numeric_slice(self.data, subset) subset = _non_reducing_slice(subset) base = 'width: 10em; height: 80%;' if not(is_list_like(color)): color = [color, color] elif len(color) == 1: color = [color[0], color[0]] elif len(color) > 2: msg = ("Must pass `color` as string or a list-like" " of length 2: [`color_negative`, `color_positive`]\n" "(eg: color=['#d65f5f', '#5fba7d'])") raise ValueError(msg) if align == 'left': self.apply(self._bar_left, subset=subset, axis=axis, color=color, width=width, base=base) elif align == 'zero': self.apply(self._bar_center_zero, subset=subset, axis=axis, color=color, width=width, base=base) elif align == 'mid': self.apply(self._bar_center_mid, subset=subset, axis=axis, color=color, width=width, base=base) else: msg = ("`align` must be one of {'left', 'zero',' mid'}") raise ValueError(msg) return self
def bar(self, subset=None, axis=0, color='#d65f5f', width=100, align='left'): """ Color the background ``color`` proptional to the values in each column. Excludes non-numeric data by default. .. versionadded:: 0.17.1 Parameters ---------- subset: IndexSlice, default None a valid slice for ``data`` to limit the style application to axis: int color: str or 2-tuple/list If a str is passed, the color is the same for both negative and positive numbers. If 2-tuple/list is used, the first element is the color_negative and the second is the color_positive (eg: ['#d65f5f', '#5fba7d']) width: float A number between 0 or 100. The largest value will cover ``width`` percent of the cell's width align : {'left', 'zero',' mid'}, default 'left' - 'left' : the min value starts at the left of the cell - 'zero' : a value of zero is located at the center of the cell - 'mid' : the center of the cell is at (max-min)/2, or if values are all negative (positive) the zero is aligned at the right (left) of the cell .. versionadded:: 0.20.0 Returns ------- self : Styler """ subset = _maybe_numeric_slice(self.data, subset) subset = _non_reducing_slice(subset) base = 'width: 10em; height: 80%;' if not (is_list_like(color)): color = [color, color] elif len(color) == 1: color = [color[0], color[0]] elif len(color) > 2: msg = ("Must pass `color` as string or a list-like" " of length 2: [`color_negative`, `color_positive`]\n" "(eg: color=['#d65f5f', '#5fba7d'])") raise ValueError(msg) if align == 'left': self.apply(self._bar_left, subset=subset, axis=axis, color=color, width=width, base=base) elif align == 'zero': self.apply(self._bar_center_zero, subset=subset, axis=axis, color=color, width=width, base=base) elif align == 'mid': self.apply(self._bar_center_mid, subset=subset, axis=axis, color=color, width=width, base=base) else: msg = ("`align` must be one of {'left', 'zero',' mid'}") raise ValueError(msg) return self
def bar(self, subset=None, axis=0, color='#d65f5f', width=100, align='left', vmin=None, vmax=None): """ Draw bar chart in the cell backgrounds. Parameters ---------- subset : IndexSlice, optional A valid slice for `data` to limit the style application to. axis : int, str or None, default 0 Apply to each column (`axis=0` or `'index'`) or to each row (`axis=1` or `'columns'`) or to the entire DataFrame at once with `axis=None`. color : str or 2-tuple/list If a str is passed, the color is the same for both negative and positive numbers. If 2-tuple/list is used, the first element is the color_negative and the second is the color_positive (eg: ['#d65f5f', '#5fba7d']). width : float, default 100 A number between 0 or 100. The largest value will cover `width` percent of the cell's width. align : {'left', 'zero',' mid'}, default 'left' How to align the bars with the cells. - 'left' : the min value starts at the left of the cell. - 'zero' : a value of zero is located at the center of the cell. - 'mid' : the center of the cell is at (max-min)/2, or if values are all negative (positive) the zero is aligned at the right (left) of the cell. .. versionadded:: 0.20.0 vmin : float, optional Minimum bar value, defining the left hand limit of the bar drawing range, lower values are clipped to `vmin`. When None (default): the minimum value of the data will be used. .. versionadded:: 0.24.0 vmax : float, optional Maximum bar value, defining the right hand limit of the bar drawing range, higher values are clipped to `vmax`. When None (default): the maximum value of the data will be used. .. versionadded:: 0.24.0 Returns ------- self : Styler """ if align not in ('left', 'zero', 'mid'): raise ValueError("`align` must be one of {'left', 'zero',' mid'}") if not (is_list_like(color)): color = [color, color] elif len(color) == 1: color = [color[0], color[0]] elif len(color) > 2: raise ValueError("`color` must be string or a list-like" " of length 2: [`color_neg`, `color_pos`]" " (eg: color=['#d65f5f', '#5fba7d'])") subset = _maybe_numeric_slice(self.data, subset) subset = _non_reducing_slice(subset) self.apply(self._bar, subset=subset, axis=axis, align=align, colors=color, width=width, vmin=vmin, vmax=vmax) return self
def background_gradient( self, cmap="PuBu", low=0, high=0, axis=0, subset=None, text_color_threshold=0.408, vmin: Optional[float] = None, vmax: Optional[float] = None, ): """ Color the background in a gradient style. The background font is determined according to the data in each column (optionally row). Requires matplotlib. Parameters ---------- cmap : str or colormap Matplotlib colormap. low : float Compress the range by the low. high : float Compress the range by the high. axis : {0 or 'index', 1 or 'columns', None}, default 0 Apply to each column (``axis=0`` or ``'index'``), to each row (``axis=1`` or ``'columns'``), or to the entire DataFrame at once with ``axis=None``. subset : IndexSlice A valid slice for ``data`` to limit the style application to. text_color_threshold : float or int Luminance threshold for determining text font. Facilitates text visibility across varying background colors. From 0 to 1. 0 = all text is dark colored, 1 = all text is light colored. .. versionadded:: 0.24.0 vmin : float, optional Minimum data value that corresponds to colormap minimum value. When None (default): the minimum value of the data will be used. .. versionadded:: 1.0.0 vmax : float, optional Maximum data value that corresponds to colormap maximum value. When None (default): the maximum value of the data will be used. .. versionadded:: 1.0.0 Returns ------- self : Styler Raises ------ ValueError If ``text_color_threshold`` is not a value from 0 to 1. Notes ----- Set ``text_color_threshold`` or tune ``low`` and ``high`` to keep the text legible by not using the entire range of the font map. The range of the data is extended by ``low * (x.max() - x.min())`` and ``high * (x.max() - x.min())`` before normalizing. """ subset = _maybe_numeric_slice(self.data, subset) subset = _non_reducing_slice(subset) self.apply( self._background_gradient, cmap=cmap, subset=subset, axis=axis, low=low, high=high, text_color_threshold=text_color_threshold, vmin=vmin, vmax=vmax, ) return self