當前位置: 首頁>>代碼示例>>Python>>正文


Python pandas.IndexSlice方法代碼示例

本文整理匯總了Python中pandas.IndexSlice方法的典型用法代碼示例。如果您正苦於以下問題:Python pandas.IndexSlice方法的具體用法?Python pandas.IndexSlice怎麽用?Python pandas.IndexSlice使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在pandas的用法示例。


在下文中一共展示了pandas.IndexSlice方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: df2list

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import IndexSlice [as 別名]
def df2list(df):
    """
    Convert a MultiIndex df to list

    Parameters
    ----------

    df : pandas.DataFrame
        A MultiIndex DataFrame where the first level is subjects and the second
        level is lists (e.g. egg.pres)

    Returns
    ----------

    lst : a list of lists of lists of values
        The input df reformatted as a list

    """
    subjects = df.index.levels[0].values.tolist()
    lists = df.index.levels[1].values.tolist()
    idx = pd.IndexSlice
    df = df.loc[idx[subjects,lists],df.columns]
    lst = [df.loc[sub,:].values.tolist() for sub in subjects]
    return lst 
開發者ID:ContextLab,項目名稱:quail,代碼行數:26,代碼來源:helpers.py

示例2: test_slice_with_negative_step

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import IndexSlice [as 別名]
def test_slice_with_negative_step(self):
        ts = Series(np.arange(20), timedelta_range('0', periods=20, freq='H'))
        SLC = pd.IndexSlice

        def assert_slices_equivalent(l_slc, i_slc):
            assert_series_equal(ts[l_slc], ts.iloc[i_slc])
            assert_series_equal(ts.loc[l_slc], ts.iloc[i_slc])
            assert_series_equal(ts.loc[l_slc], ts.iloc[i_slc])

        assert_slices_equivalent(SLC[Timedelta(hours=7)::-1], SLC[7::-1])
        assert_slices_equivalent(SLC['7 hours'::-1], SLC[7::-1])

        assert_slices_equivalent(SLC[:Timedelta(hours=7):-1], SLC[:6:-1])
        assert_slices_equivalent(SLC[:'7 hours':-1], SLC[:6:-1])

        assert_slices_equivalent(SLC['15 hours':'7 hours':-1], SLC[15:6:-1])
        assert_slices_equivalent(SLC[Timedelta(hours=15):Timedelta(hours=7):-
                                     1], SLC[15:6:-1])
        assert_slices_equivalent(SLC['15 hours':Timedelta(hours=7):-1],
                                 SLC[15:6:-1])
        assert_slices_equivalent(SLC[Timedelta(hours=15):'7 hours':-1],
                                 SLC[15:6:-1])

        assert_slices_equivalent(SLC['7 hours':'15 hours':-1], SLC[:0]) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:26,代碼來源:test_partial_slicing.py

示例3: test_slice_with_negative_step

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import IndexSlice [as 別名]
def test_slice_with_negative_step(self):
        ts = Series(np.arange(20),
                    period_range('2014-01', periods=20, freq='M'))
        SLC = pd.IndexSlice

        def assert_slices_equivalent(l_slc, i_slc):
            tm.assert_series_equal(ts[l_slc], ts.iloc[i_slc])
            tm.assert_series_equal(ts.loc[l_slc], ts.iloc[i_slc])
            tm.assert_series_equal(ts.loc[l_slc], ts.iloc[i_slc])

        assert_slices_equivalent(SLC[Period('2014-10')::-1], SLC[9::-1])
        assert_slices_equivalent(SLC['2014-10'::-1], SLC[9::-1])

        assert_slices_equivalent(SLC[:Period('2014-10'):-1], SLC[:8:-1])
        assert_slices_equivalent(SLC[:'2014-10':-1], SLC[:8:-1])

        assert_slices_equivalent(SLC['2015-02':'2014-10':-1], SLC[13:8:-1])
        assert_slices_equivalent(SLC[Period('2015-02'):Period('2014-10'):-1],
                                 SLC[13:8:-1])
        assert_slices_equivalent(SLC['2015-02':Period('2014-10'):-1],
                                 SLC[13:8:-1])
        assert_slices_equivalent(SLC[Period('2015-02'):'2014-10':-1],
                                 SLC[13:8:-1])

        assert_slices_equivalent(SLC['2014-10':'2015-02':-1], SLC[:0]) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:27,代碼來源:test_partial_slicing.py

示例4: test_str_label_slicing_with_negative_step

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import IndexSlice [as 別名]
def test_str_label_slicing_with_negative_step(self):
        SLC = pd.IndexSlice

        def assert_slices_equivalent(l_slc, i_slc):
            tm.assert_series_equal(s.loc[l_slc], s.iloc[i_slc])

            if not idx.is_integer:
                # For integer indices, ix and plain getitem are position-based.
                tm.assert_series_equal(s[l_slc], s.iloc[i_slc])
                tm.assert_series_equal(s.loc[l_slc], s.iloc[i_slc])

        for idx in [_mklbl('A', 20), np.arange(20) + 100,
                    np.linspace(100, 150, 20)]:
            idx = Index(idx)
            s = Series(np.arange(20), index=idx)
            assert_slices_equivalent(SLC[idx[9]::-1], SLC[9::-1])
            assert_slices_equivalent(SLC[:idx[9]:-1], SLC[:8:-1])
            assert_slices_equivalent(SLC[idx[13]:idx[9]:-1], SLC[13:8:-1])
            assert_slices_equivalent(SLC[idx[9]:idx[13]:-1], SLC[:0]) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:21,代碼來源:test_indexing.py

示例5: test_non_reducing_slice_on_multiindex

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import IndexSlice [as 別名]
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 = pd.DataFrame(dic, index=[0, 1])
        idx = pd.IndexSlice
        slice_ = idx[:, idx['b', 'd']]
        tslice_ = _non_reducing_slice(slice_)

        result = df.loc[tslice_]
        expected = pd.DataFrame({('b', 'd'): [4, 1]})
        tm.assert_frame_equal(result, expected) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:18,代碼來源:test_slice.py

示例6: test_applymap_subset

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import IndexSlice [as 別名]
def test_applymap_subset(self):
        def f(x):
            return 'foo: bar'

        slices = [pd.IndexSlice[:], pd.IndexSlice[:, ['A']],
                  pd.IndexSlice[[1], :], pd.IndexSlice[[1], ['A']],
                  pd.IndexSlice[:2, ['A', 'B']]]

        for slice_ in slices:
            result = self.df.style.applymap(f, subset=slice_)._compute().ctx
            expected = {(r, c): ['foo: bar']
                        for r, row in enumerate(self.df.index)
                        for c, col in enumerate(self.df.columns)
                        if row in self.df.loc[slice_].index and
                        col in self.df.loc[slice_].columns}
            assert result == expected 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:18,代碼來源:test_style.py

示例7: test_where_subset

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import IndexSlice [as 別名]
def test_where_subset(self):
        # GH 17474
        def f(x):
            return x > 0.5

        style1 = 'foo: bar'
        style2 = 'baz: foo'

        slices = [pd.IndexSlice[:], pd.IndexSlice[:, ['A']],
                  pd.IndexSlice[[1], :], pd.IndexSlice[[1], ['A']],
                  pd.IndexSlice[:2, ['A', 'B']]]

        for slice_ in slices:
            result = self.df.style.where(f, style1, style2,
                                         subset=slice_)._compute().ctx
            expected = {(r, c):
                        [style1 if f(self.df.loc[row, col]) else style2]
                        for r, row in enumerate(self.df.index)
                        for c, col in enumerate(self.df.columns)
                        if row in self.df.loc[slice_].index and
                        col in self.df.loc[slice_].columns}
            assert result == expected 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:24,代碼來源:test_style.py

示例8: test_where_subset_compare_with_applymap

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import IndexSlice [as 別名]
def test_where_subset_compare_with_applymap(self):
        # GH 17474
        def f(x):
            return x > 0.5

        style1 = 'foo: bar'
        style2 = 'baz: foo'

        def g(x):
            return style1 if f(x) else style2

        slices = [pd.IndexSlice[:], pd.IndexSlice[:, ['A']],
                  pd.IndexSlice[[1], :], pd.IndexSlice[[1], ['A']],
                  pd.IndexSlice[:2, ['A', 'B']]]

        for slice_ in slices:
            result = self.df.style.where(f, style1, style2,
                                         subset=slice_)._compute().ctx
            expected = self.df.style.applymap(g, subset=slice_)._compute().ctx
            assert result == expected 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:22,代碼來源:test_style.py

示例9: test_bad_apply_shape

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import IndexSlice [as 別名]
def test_bad_apply_shape(self):
        df = pd.DataFrame([[1, 2], [3, 4]])
        with pytest.raises(ValueError):
            df.style._apply(lambda x: 'x', subset=pd.IndexSlice[[0, 1], :])

        with pytest.raises(ValueError):
            df.style._apply(lambda x: [''], subset=pd.IndexSlice[[0, 1], :])

        with pytest.raises(ValueError):
            df.style._apply(lambda x: ['', '', '', ''])

        with pytest.raises(ValueError):
            df.style._apply(lambda x: ['', '', ''], subset=1)

        with pytest.raises(ValueError):
            df.style._apply(lambda x: ['', '', ''], axis=1) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:18,代碼來源:test_style.py

示例10: hide_columns

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import IndexSlice [as 別名]
def hide_columns(self, subset):
        """
        Hide columns from rendering.

        .. versionadded:: 0.23.0

        Parameters
        ----------
        subset : IndexSlice
            An argument to ``DataFrame.loc`` that identifies which columns
            are hidden.

        Returns
        -------
        self : Styler
        """
        subset = _non_reducing_slice(subset)
        hidden_df = self.data.loc[subset]
        self.hidden_columns = self.columns.get_indexer_for(hidden_df.columns)
        return self

    # -----------------------------------------------------------------------
    # A collection of "builtin" styles
    # ----------------------------------------------------------------------- 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:26,代碼來源:style.py

示例11: highlight_max

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import IndexSlice [as 別名]
def highlight_max(self, subset=None, color='yellow', axis=0):
        """
        Highlight the maximum by shading the background.

        Parameters
        ----------
        subset : IndexSlice, default None
            a valid slice for ``data`` to limit the style application to
        color : str, default 'yellow'
        axis : int, str, or None; default 0
            0 or 'index' for columnwise (default), 1 or 'columns' for rowwise,
            or ``None`` for tablewise

        Returns
        -------
        self : Styler
        """
        return self._highlight_handler(subset=subset, color=color, axis=axis,
                                       max_=True) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:21,代碼來源:style.py

示例12: highlight_min

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import IndexSlice [as 別名]
def highlight_min(self, subset=None, color='yellow', axis=0):
        """
        Highlight the minimum by shading the background.

        Parameters
        ----------
        subset : IndexSlice, default None
            a valid slice for ``data`` to limit the style application to
        color : str, default 'yellow'
        axis : int, str, or None; default 0
            0 or 'index' for columnwise (default), 1 or 'columns' for rowwise,
            or ``None`` for tablewise

        Returns
        -------
        self : Styler
        """
        return self._highlight_handler(subset=subset, color=color, axis=axis,
                                       max_=False) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:21,代碼來源:style.py

示例13: test_applymap_subset

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import IndexSlice [as 別名]
def test_applymap_subset(self):
        def f(x):
            return 'foo: bar'

        slices = [pd.IndexSlice[:], pd.IndexSlice[:, ['A']],
                  pd.IndexSlice[[1], :], pd.IndexSlice[[1], ['A']],
                  pd.IndexSlice[:2, ['A', 'B']]]

        for slice_ in slices:
            result = self.df.style.applymap(f, subset=slice_)._compute().ctx
            expected = dict(((r, c), ['foo: bar'])
                            for r, row in enumerate(self.df.index)
                            for c, col in enumerate(self.df.columns)
                            if row in self.df.loc[slice_].index and
                            col in self.df.loc[slice_].columns)
            assert result == expected 
開發者ID:birforce,項目名稱:vnpy_crypto,代碼行數:18,代碼來源:test_style.py

示例14: test_where_subset

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import IndexSlice [as 別名]
def test_where_subset(self):
        # GH 17474
        def f(x):
            return x > 0.5

        style1 = 'foo: bar'
        style2 = 'baz: foo'

        slices = [pd.IndexSlice[:], pd.IndexSlice[:, ['A']],
                  pd.IndexSlice[[1], :], pd.IndexSlice[[1], ['A']],
                  pd.IndexSlice[:2, ['A', 'B']]]

        for slice_ in slices:
            result = self.df.style.where(f, style1, style2,
                                         subset=slice_)._compute().ctx
            expected = dict(((r, c),
                            [style1 if f(self.df.loc[row, col]) else style2])
                            for r, row in enumerate(self.df.index)
                            for c, col in enumerate(self.df.columns)
                            if row in self.df.loc[slice_].index and
                            col in self.df.loc[slice_].columns)
            assert result == expected 
開發者ID:birforce,項目名稱:vnpy_crypto,代碼行數:24,代碼來源:test_style.py

示例15: hide_columns

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import IndexSlice [as 別名]
def hide_columns(self, subset):
        """
        Hide columns from rendering.

        .. versionadded:: 0.23.0

        Parameters
        ----------
        subset: IndexSlice
            An argument to ``DataFrame.loc`` that identifies which columns
            are hidden.

        Returns
        -------
        self : Styler
        """
        subset = _non_reducing_slice(subset)
        hidden_df = self.data.loc[subset]
        self.hidden_columns = self.columns.get_indexer_for(hidden_df.columns)
        return self

    # -----------------------------------------------------------------------
    # A collection of "builtin" styles
    # ----------------------------------------------------------------------- 
開發者ID:birforce,項目名稱:vnpy_crypto,代碼行數:26,代碼來源:style.py


注:本文中的pandas.IndexSlice方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。