Example #1
0
    def test_non_reducing_multi_slice_on_multiindex(self, slice_):
        # GH 33562
        cols = pd.MultiIndex.from_product([["a", "b"], ["c", "d"], ["e", "f"]])
        idxs = pd.MultiIndex.from_product([["U", "V"], ["W", "X"], ["Y", "Z"]])
        df = DataFrame(np.arange(64).reshape(8, 8), columns=cols, index=idxs)

        expected = df.loc[slice_]
        result = df.loc[_non_reducing_slice(slice_)]
        tm.assert_frame_equal(result, expected)
Example #2
0
    def test_list_slice(self, box):
        # like dataframe getitem
        subset = box(["A"])

        df = DataFrame({"A": [1, 2], "B": [3, 4]}, index=["A", "B"])
        expected = pd.IndexSlice[:, ["A"]]

        result = _non_reducing_slice(subset)
        tm.assert_frame_equal(df.loc[result], df.loc[expected])
Example #3
0
    def test_non_reducing_slice_on_multiindex(self):
        # GH 19861
        dic = {
            ("a", "d"): [1, 4],
            ("a", "c"): [2, 3],
            ("b", "c"): [3, 2],
            ("b", "d"): [4, 1],
        }
        df = DataFrame(dic, index=[0, 1])
        idx = pd.IndexSlice
        slice_ = idx[:, idx["b", "d"]]
        tslice_ = _non_reducing_slice(slice_)

        result = df.loc[tslice_]
        expected = DataFrame({("b", "d"): [4, 1]})
        tm.assert_frame_equal(result, expected)
Example #4
0
    def absolute_background_gradient(
        self,
        cmap="YlOrRd",
        low=0,
        high=0,
        axis=0,
        subset=None,
        text_color_threshold=0.408,
        vmin: Optional[float] = 0,
        vmax: Optional[float] = None,
    ):
        """
		Color the background in a gradient style,
		based on the magnitude (absolute value).
		The background color 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 color. Facilitates text
			visibility across varying background colors. From 0 to 1.
			0 = all text is dark colored, 1 = all text is light colored.
		vmin : float, default 0
			Minimum data value that corresponds to colormap minimum value.
			When None, the minimum value of the data will be used.
		vmax : float, optional
			Maximum data value that corresponds to colormap maximum value.
			When None (default): the maximum value of the data will be used.
		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._absolute_background_gradient,
            cmap=cmap,
            subset=subset,
            axis=axis,
            low=low,
            high=high,
            text_color_threshold=text_color_threshold,
            vmin=vmin,
            vmax=vmax,
        )
        return self
Example #5
0
    def test_non_reducing_slice(self, slc):
        df = DataFrame([[0, 1], [2, 3]])

        tslice_ = _non_reducing_slice(slc)
        assert isinstance(df.loc[tslice_], DataFrame)