当前位置: 首页>>代码示例>>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;未经允许,请勿转载。