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


Python pandas.option_context方法代碼示例

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


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

示例1: test_sparse_mi_max_row

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import option_context [as 別名]
def test_sparse_mi_max_row(self):
        idx = pd.MultiIndex.from_tuples([('A', 0), ('A', 1), ('B', 0),
                                         ('C', 0), ('C', 1), ('C', 2)])
        s = pd.Series([1, np.nan, np.nan, 3, np.nan, np.nan],
                      index=idx).to_sparse()
        result = repr(s)
        dfm = self.dtype_format_for_platform
        exp = ("A  0    1.0\n   1    NaN\nB  0    NaN\n"
               "C  0    3.0\n   1    NaN\n   2    NaN\n"
               "dtype: Sparse[float64, nan]\nBlockIndex\n"
               "Block locations: array([0, 3]{0})\n"
               "Block lengths: array([1, 1]{0})".format(dfm))
        assert result == exp

        with option_context("display.max_rows", 3,
                            "display.show_dimensions", False):
            # GH 13144
            result = repr(s)
            exp = ("A  0    1.0\n       ... \nC  2    NaN\n"
                   "dtype: Sparse[float64, nan]\nBlockIndex\n"
                   "Block locations: array([0, 3]{0})\n"
                   "Block lengths: array([1, 1]{0})".format(dfm))
            assert result == exp 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:25,代碼來源:test_format.py

示例2: test_sparse_bool

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import option_context [as 別名]
def test_sparse_bool(self):
        # GH 13110
        s = pd.SparseSeries([True, False, False, True, False, False],
                            fill_value=False)
        result = repr(s)
        dtype = '' if use_32bit_repr else ', dtype=int32'
        exp = ("0     True\n1    False\n2    False\n"
               "3     True\n4    False\n5    False\n"
               "dtype: Sparse[bool, False]\nBlockIndex\n"
               "Block locations: array([0, 3]{0})\n"
               "Block lengths: array([1, 1]{0})".format(dtype))
        assert result == exp

        with option_context("display.max_rows", 3):
            result = repr(s)
            exp = ("0     True\n     ...  \n5    False\n"
                   "Length: 6, dtype: Sparse[bool, False]\nBlockIndex\n"
                   "Block locations: array([0, 3]{0})\n"
                   "Block lengths: array([1, 1]{0})".format(dtype))
            assert result == exp 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:22,代碼來源:test_format.py

示例3: test_sparse_int

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import option_context [as 別名]
def test_sparse_int(self):
        # GH 13110
        s = pd.SparseSeries([0, 1, 0, 0, 1, 0], fill_value=False)

        result = repr(s)
        dtype = '' if use_32bit_repr else ', dtype=int32'
        exp = ("0    0\n1    1\n2    0\n3    0\n4    1\n"
               "5    0\ndtype: Sparse[int64, False]\nBlockIndex\n"
               "Block locations: array([1, 4]{0})\n"
               "Block lengths: array([1, 1]{0})".format(dtype))
        assert result == exp

        with option_context("display.max_rows", 3,
                            "display.show_dimensions", False):
            result = repr(s)
            exp = ("0    0\n    ..\n5    0\n"
                   "dtype: Sparse[int64, False]\nBlockIndex\n"
                   "Block locations: array([1, 4]{0})\n"
                   "Block lengths: array([1, 1]{0})".format(dtype))
            assert result == exp 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:22,代碼來源:test_format.py

示例4: test_latex_repr

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import option_context [as 別名]
def test_latex_repr(self):
        result = r"""\begin{tabular}{ll}
\toprule
{} &         0 \\
\midrule
0 &  $\alpha$ \\
1 &         b \\
2 &         c \\
\bottomrule
\end{tabular}
"""
        with option_context('display.latex.escape', False,
                            'display.latex.repr', True):
            s = Series([r'$\alpha$', 'b', 'c'])
            assert result == s._repr_latex_()

        assert s._repr_latex_() is None 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:19,代碼來源:test_repr.py

示例5: test_categorical_repr

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import option_context [as 別名]
def test_categorical_repr(self):
        a = Series(Categorical([1, 2, 3, 4]))
        exp = u("0    1\n1    2\n2    3\n3    4\n" +
                "dtype: category\nCategories (4, int64): [1, 2, 3, 4]")

        assert exp == a.__unicode__()

        a = Series(Categorical(["a", "b"] * 25))
        exp = u("0     a\n1     b\n" + "     ..\n" + "48    a\n49    b\n" +
                "Length: 50, dtype: category\nCategories (2, object): [a, b]")
        with option_context("display.max_rows", 5):
            assert exp == repr(a)

        levs = list("abcdefghijklmnopqrstuvwxyz")
        a = Series(Categorical(["a", "b"], categories=levs, ordered=True))
        exp = u("0    a\n1    b\n" + "dtype: category\n"
                "Categories (26, object): [a < b < c < d ... w < x < y < z]")
        assert exp == a.__unicode__() 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:20,代碼來源:test_repr.py

示例6: test_var_std

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import option_context [as 別名]
def test_var_std(self, float_frame_with_na, datetime_frame, float_frame,
                     float_string_frame):
        alt = lambda x: np.var(x, ddof=1)
        assert_stat_op_calc('var', alt, float_frame_with_na)
        assert_stat_op_api('var', float_frame, float_string_frame)

        alt = lambda x: np.std(x, ddof=1)
        assert_stat_op_calc('std', alt, float_frame_with_na)
        assert_stat_op_api('std', float_frame, float_string_frame)

        result = datetime_frame.std(ddof=4)
        expected = datetime_frame.apply(lambda x: x.std(ddof=4))
        tm.assert_almost_equal(result, expected)

        result = datetime_frame.var(ddof=4)
        expected = datetime_frame.apply(lambda x: x.var(ddof=4))
        tm.assert_almost_equal(result, expected)

        arr = np.repeat(np.random.random((1, 1000)), 1000, 0)
        result = nanops.nanvar(arr, axis=0)
        assert not (result < 0).any()

        with pd.option_context('use_bottleneck', False):
            result = nanops.nanvar(arr, axis=0)
            assert not (result < 0).any() 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:27,代碼來源:test_analytics.py

示例7: test_sem

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import option_context [as 別名]
def test_sem(self, float_frame_with_na, datetime_frame,
                 float_frame, float_string_frame):
        alt = lambda x: np.std(x, ddof=1) / np.sqrt(len(x))
        assert_stat_op_calc('sem', alt, float_frame_with_na)
        assert_stat_op_api('sem', float_frame, float_string_frame)

        result = datetime_frame.sem(ddof=4)
        expected = datetime_frame.apply(
            lambda x: x.std(ddof=4) / np.sqrt(len(x)))
        tm.assert_almost_equal(result, expected)

        arr = np.repeat(np.random.random((1, 1000)), 1000, 0)
        result = nanops.nansem(arr, axis=0)
        assert not (result < 0).any()

        with pd.option_context('use_bottleneck', False):
            result = nanops.nansem(arr, axis=0)
            assert not (result < 0).any() 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:20,代碼來源:test_analytics.py

示例8: test_idxminmax_with_inf

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import option_context [as 別名]
def test_idxminmax_with_inf(self):
        # For numeric data with NA and Inf (GH #13595)
        s = pd.Series([0, -np.inf, np.inf, np.nan])

        assert s.idxmin() == 1
        assert np.isnan(s.idxmin(skipna=False))

        assert s.idxmax() == 2
        assert np.isnan(s.idxmax(skipna=False))

        # Using old-style behavior that treats floating point nan, -inf, and
        # +inf as missing
        with pd.option_context('mode.use_inf_as_na', True):
            assert s.idxmin() == 0
            assert np.isnan(s.idxmin(skipna=False))
            assert s.idxmax() == 0
            np.isnan(s.idxmax(skipna=False)) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:19,代碼來源:test_reductions.py

示例9: test_show_null_counts

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import option_context [as 別名]
def test_show_null_counts(self):

        df = DataFrame(1, columns=range(10), index=range(10))
        df.iloc[1, 1] = np.nan

        def check(null_counts, result):
            buf = StringIO()
            df.info(buf=buf, null_counts=null_counts)
            assert ('non-null' in buf.getvalue()) is result

        with option_context('display.max_info_rows', 20,
                            'display.max_info_columns', 20):
            check(None, True)
            check(True, True)
            check(False, False)

        with option_context('display.max_info_rows', 5,
                            'display.max_info_columns', 5):
            check(None, False)
            check(True, False)
            check(False, False) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:23,代碼來源:test_format.py

示例10: test_repr_truncation

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import option_context [as 別名]
def test_repr_truncation(self):
        max_len = 20
        with option_context("display.max_colwidth", max_len):
            df = DataFrame({'A': np.random.randn(10),
                            'B': [tm.rands(np.random.randint(
                                max_len - 1, max_len + 1)) for i in range(10)
            ]})
            r = repr(df)
            r = r[r.find('\n') + 1:]

            adj = fmt._get_adjustment()

            for line, value in lzip(r.split('\n'), df['B']):
                if adj.len(value) + 1 > max_len:
                    assert '...' in line
                else:
                    assert '...' not in line

        with option_context("display.max_colwidth", 999999):
            assert '...' not in repr(df)

        with option_context("display.max_colwidth", max_len + 2):
            assert '...' not in repr(df) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:25,代碼來源:test_format.py

示例11: test_expand_frame_repr

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import option_context [as 別名]
def test_expand_frame_repr(self):
        df_small = DataFrame('hello', [0], [0])
        df_wide = DataFrame('hello', [0], lrange(10))
        df_tall = DataFrame('hello', lrange(30), lrange(5))

        with option_context('mode.sim_interactive', True):
            with option_context('display.max_columns', 10, 'display.width', 20,
                                'display.max_rows', 20,
                                'display.show_dimensions', True):
                with option_context('display.expand_frame_repr', True):
                    assert not has_truncated_repr(df_small)
                    assert not has_expanded_repr(df_small)
                    assert not has_truncated_repr(df_wide)
                    assert has_expanded_repr(df_wide)
                    assert has_vertically_truncated_repr(df_tall)
                    assert has_expanded_repr(df_tall)

                with option_context('display.expand_frame_repr', False):
                    assert not has_truncated_repr(df_small)
                    assert not has_expanded_repr(df_small)
                    assert not has_horizontally_truncated_repr(df_wide)
                    assert not has_expanded_repr(df_wide)
                    assert has_vertically_truncated_repr(df_tall)
                    assert not has_expanded_repr(df_tall) 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:26,代碼來源:test_format.py

示例12: test_str_max_colwidth

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import option_context [as 別名]
def test_str_max_colwidth(self):
        # GH 7856
        df = pd.DataFrame([{'a': 'foo',
                            'b': 'bar',
                            'c': 'uncomfortably long line with lots of stuff',
                            'd': 1}, {'a': 'foo',
                                      'b': 'bar',
                                      'c': 'stuff',
                                      'd': 1}])
        df.set_index(['a', 'b', 'c'])
        assert str(df) == (
            '     a    b                                           c  d\n'
            '0  foo  bar  uncomfortably long line with lots of stuff  1\n'
            '1  foo  bar                                       stuff  1')
        with option_context('max_colwidth', 20):
            assert str(df) == ('     a    b                    c  d\n'
                               '0  foo  bar  uncomfortably lo...  1\n'
                               '1  foo  bar                stuff  1') 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:20,代碼來源:test_format.py

示例13: test_truncate_with_different_dtypes

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import option_context [as 別名]
def test_truncate_with_different_dtypes(self):

        # 11594, 12045
        # when truncated the dtypes of the splits can differ

        # 11594
        import datetime
        s = Series([datetime.datetime(2012, 1, 1)] * 10 +
                   [datetime.datetime(1012, 1, 2)] + [
            datetime.datetime(2012, 1, 3)] * 10)

        with pd.option_context('display.max_rows', 8):
            result = str(s)
            assert 'object' in result

        # 12045
        df = DataFrame({'text': ['some words'] + [None] * 9})

        with pd.option_context('display.max_rows', 8,
                               'display.max_columns', 3):
            result = str(df)
            assert 'None' in result
            assert 'NaN' not in result 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:25,代碼來源:test_format.py

示例14: test_wide_repr

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import option_context [as 別名]
def test_wide_repr(self):
        with option_context('mode.sim_interactive', True,
                            'display.show_dimensions', True,
                            'display.max_columns', 20):
            max_cols = get_option('display.max_columns')
            df = DataFrame(tm.rands_array(25, size=(10, max_cols - 1)))
            set_option('display.expand_frame_repr', False)
            rep_str = repr(df)

            assert "10 rows x {c} columns".format(c=max_cols - 1) in rep_str
            set_option('display.expand_frame_repr', True)
            wide_repr = repr(df)
            assert rep_str != wide_repr

            with option_context('display.width', 120):
                wider_repr = repr(df)
                assert len(wider_repr) < len(wide_repr)

        reset_option('display.expand_frame_repr') 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:21,代碼來源:test_format.py

示例15: test_wide_repr_named

# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import option_context [as 別名]
def test_wide_repr_named(self):
        with option_context('mode.sim_interactive', True,
                            'display.max_columns', 20):
            max_cols = get_option('display.max_columns')
            df = DataFrame(tm.rands_array(25, size=(10, max_cols - 1)))
            df.index.name = 'DataFrame Index'
            set_option('display.expand_frame_repr', False)

            rep_str = repr(df)
            set_option('display.expand_frame_repr', True)
            wide_repr = repr(df)
            assert rep_str != wide_repr

            with option_context('display.width', 150):
                wider_repr = repr(df)
                assert len(wider_repr) < len(wide_repr)

            for line in wide_repr.splitlines()[1::13]:
                assert 'DataFrame Index' in line

        reset_option('display.expand_frame_repr') 
開發者ID:Frank-qlu,項目名稱:recruit,代碼行數:23,代碼來源:test_format.py


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