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


Python style.Styler類代碼示例

本文整理匯總了Python中pandas.formats.style.Styler的典型用法代碼示例。如果您正苦於以下問題:Python Styler類的具體用法?Python Styler怎麽用?Python Styler使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


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

示例1: test_table_attributes

    def test_table_attributes(self):
        attributes = 'class="foo" data-bar'
        styler = Styler(self.df, table_attributes=attributes)
        result = styler.render()
        self.assertTrue('class="foo" data-bar' in result)

        result = self.df.style.set_table_attributes(attributes).render()
        self.assertTrue('class="foo" data-bar' in result)
開發者ID:ivannz,項目名稱:pandas,代碼行數:8,代碼來源:test_style.py

示例2: test_uuid

    def test_uuid(self):
        styler = Styler(self.df, uuid='abc123')
        result = styler.render()
        self.assertTrue('abc123' in result)

        styler = self.df.style
        result = styler.set_uuid('aaa')
        self.assertTrue(result is styler)
        self.assertEqual(result.uuid, 'aaa')
開發者ID:ivannz,項目名稱:pandas,代碼行數:9,代碼來源:test_style.py

示例3: test_caption

    def test_caption(self):
        styler = Styler(self.df, caption='foo')
        result = styler.render()
        self.assertTrue(all(['caption' in result, 'foo' in result]))

        styler = self.df.style
        result = styler.set_caption('baz')
        self.assertTrue(styler is result)
        self.assertEqual(styler.caption, 'baz')
開發者ID:ivannz,項目名稱:pandas,代碼行數:9,代碼來源:test_style.py

示例4: test_precision

    def test_precision(self):
        with pd.option_context('display.precision', 10):
            s = Styler(self.df)
        self.assertEqual(s.precision, 10)
        s = Styler(self.df, precision=2)
        self.assertEqual(s.precision, 2)

        s2 = s.set_precision(4)
        self.assertTrue(s is s2)
        self.assertEqual(s.precision, 4)
開發者ID:ivannz,項目名稱:pandas,代碼行數:10,代碼來源:test_style.py

示例5: test_table_styles

    def test_table_styles(self):
        style = [{'selector': 'th', 'props': [('foo', 'bar')]}]
        styler = Styler(self.df, table_styles=style)
        result = ' '.join(styler.render().split())
        self.assertTrue('th { foo: bar; }' in result)

        styler = self.df.style
        result = styler.set_table_styles(style)
        self.assertTrue(styler is result)
        self.assertEqual(styler.table_styles, style)
開發者ID:ivannz,項目名稱:pandas,代碼行數:10,代碼來源:test_style.py

示例6: configure_properties

    def configure_properties(self):

        Styler.__init__(self, self.data)

        self.applymap(styler_utils.center)
        self.applymap(lambda x: 'padding: %s' % self.padding)
        self.set_table_styles(default_table_styles)
        self.set_properties(**{'border': '1px solid black'})

        if self.preset:
            self.apply_preset()
        if self.auto_format:
            self.value_formatter()
開發者ID:glentennis,項目名稱:general-code,代碼行數:13,代碼來源:core.py

示例7: setUp

    def setUp(self):
        np.random.seed(24)
        self.s = DataFrame({"A": np.random.permutation(range(6))})
        self.df = DataFrame({"A": [0, 1], "B": np.random.randn(2)})
        self.f = lambda x: x
        self.g = lambda x: x

        def h(x, foo="bar"):
            return pd.Series(["color: %s" % foo], index=x.index, name=x.name)

        self.h = h
        self.styler = Styler(self.df)
        self.attrs = pd.DataFrame({"A": ["color: red", "color: blue"]})
        self.dataframes = [self.df, pd.DataFrame({"f": [1.0, 2.0], "o": ["a", "b"], "c": pd.Categorical(["a", "b"])})]
開發者ID:ChristopherShort,項目名稱:pandas,代碼行數:14,代碼來源:test_style.py

示例8: setUp

    def setUp(self):
        np.random.seed(24)
        self.s = DataFrame({'A': np.random.permutation(range(6))})
        self.df = DataFrame({'A': [0, 1], 'B': np.random.randn(2)})
        self.f = lambda x: x
        self.g = lambda x: x

        def h(x, foo='bar'):
            return pd.Series(['color: %s' % foo], index=x.index, name=x.name)

        self.h = h
        self.styler = Styler(self.df)
        self.attrs = pd.DataFrame({'A': ['color: red', 'color: blue']})
        self.dataframes = [
            self.df,
            pd.DataFrame({'f': [1., 2.], 'o': ['a', 'b'],
                          'c': pd.Categorical(['a', 'b'])})
        ]
開發者ID:ivannz,項目名稱:pandas,代碼行數:18,代碼來源:test_style.py

示例9: TestStyler

class TestStyler(TestCase):

    def setUp(self):
        np.random.seed(24)
        self.s = DataFrame({'A': np.random.permutation(range(6))})
        self.df = DataFrame({'A': [0, 1], 'B': np.random.randn(2)})
        self.f = lambda x: x
        self.g = lambda x: x

        def h(x, foo='bar'):
            return pd.Series(['color: %s' % foo], index=x.index, name=x.name)

        self.h = h
        self.styler = Styler(self.df)
        self.attrs = pd.DataFrame({'A': ['color: red', 'color: blue']})
        self.dataframes = [
            self.df,
            pd.DataFrame({'f': [1., 2.], 'o': ['a', 'b'],
                          'c': pd.Categorical(['a', 'b'])})
        ]

    def test_init_non_pandas(self):
        with tm.assertRaises(TypeError):
            Styler([1, 2, 3])

    def test_init_series(self):
        result = Styler(pd.Series([1, 2]))
        self.assertEqual(result.data.ndim, 2)

    def test_repr_html_ok(self):
        self.styler._repr_html_()

    def test_update_ctx(self):
        self.styler._update_ctx(self.attrs)
        expected = {(0, 0): ['color: red'],
                    (1, 0): ['color: blue']}
        self.assertEqual(self.styler.ctx, expected)

    def test_update_ctx_flatten_multi(self):
        attrs = DataFrame({"A": ['color: red; foo: bar',
                                 'color: blue; foo: baz']})
        self.styler._update_ctx(attrs)
        expected = {(0, 0): ['color: red', ' foo: bar'],
                    (1, 0): ['color: blue', ' foo: baz']}
        self.assertEqual(self.styler.ctx, expected)

    def test_update_ctx_flatten_multi_traliing_semi(self):
        attrs = DataFrame({"A": ['color: red; foo: bar;',
                                 'color: blue; foo: baz;']})
        self.styler._update_ctx(attrs)
        expected = {(0, 0): ['color: red', ' foo: bar'],
                    (1, 0): ['color: blue', ' foo: baz']}
        self.assertEqual(self.styler.ctx, expected)

    def test_copy(self):
        s2 = copy.copy(self.styler)
        self.assertTrue(self.styler is not s2)
        self.assertTrue(self.styler.ctx is s2.ctx)  # shallow
        self.assertTrue(self.styler._todo is s2._todo)

        self.styler._update_ctx(self.attrs)
        self.styler.highlight_max()
        self.assertEqual(self.styler.ctx, s2.ctx)
        self.assertEqual(self.styler._todo, s2._todo)

    def test_deepcopy(self):
        s2 = copy.deepcopy(self.styler)
        self.assertTrue(self.styler is not s2)
        self.assertTrue(self.styler.ctx is not s2.ctx)
        self.assertTrue(self.styler._todo is not s2._todo)

        self.styler._update_ctx(self.attrs)
        self.styler.highlight_max()
        self.assertNotEqual(self.styler.ctx, s2.ctx)
        self.assertEqual(s2._todo, [])
        self.assertNotEqual(self.styler._todo, s2._todo)

    def test_clear(self):
        s = self.df.style.highlight_max()._compute()
        self.assertTrue(len(s.ctx) > 0)
        self.assertTrue(len(s._todo) > 0)
        s.clear()
        self.assertTrue(len(s.ctx) == 0)
        self.assertTrue(len(s._todo) == 0)

    def test_render(self):
        df = pd.DataFrame({"A": [0, 1]})
        style = lambda x: pd.Series(["color: red", "color: blue"], name=x.name)
        s = Styler(df, uuid='AB').apply(style)
        s.render()
        # it worked?

    def test_render_double(self):
        df = pd.DataFrame({"A": [0, 1]})
        style = lambda x: pd.Series(["color: red; border: 1px",
                                     "color: blue; border: 2px"], name=x.name)
        s = Styler(df, uuid='AB').apply(style)
        s.render()
        # it worked?

#.........這裏部分代碼省略.........
開發者ID:ivannz,項目名稱:pandas,代碼行數:101,代碼來源:test_style.py

示例10: test_render_double

 def test_render_double(self):
     df = pd.DataFrame({"A": [0, 1]})
     style = lambda x: pd.Series(["color: red; border: 1px",
                                  "color: blue; border: 2px"], name=x.name)
     s = Styler(df, uuid='AB').apply(style)
     s.render()
開發者ID:ivannz,項目名稱:pandas,代碼行數:6,代碼來源:test_style.py

示例11: TestStyler

class TestStyler(TestCase):
    def setUp(self):
        np.random.seed(24)
        self.s = DataFrame({"A": np.random.permutation(range(6))})
        self.df = DataFrame({"A": [0, 1], "B": np.random.randn(2)})
        self.f = lambda x: x
        self.g = lambda x: x

        def h(x, foo="bar"):
            return pd.Series(["color: %s" % foo], index=x.index, name=x.name)

        self.h = h
        self.styler = Styler(self.df)
        self.attrs = pd.DataFrame({"A": ["color: red", "color: blue"]})
        self.dataframes = [self.df, pd.DataFrame({"f": [1.0, 2.0], "o": ["a", "b"], "c": pd.Categorical(["a", "b"])})]

    def test_init_non_pandas(self):
        with tm.assertRaises(TypeError):
            Styler([1, 2, 3])

    def test_init_series(self):
        result = Styler(pd.Series([1, 2]))
        self.assertEqual(result.data.ndim, 2)

    def test_repr_html_ok(self):
        self.styler._repr_html_()

    def test_update_ctx(self):
        self.styler._update_ctx(self.attrs)
        expected = {(0, 0): ["color: red"], (1, 0): ["color: blue"]}
        self.assertEqual(self.styler.ctx, expected)

    def test_update_ctx_flatten_multi(self):
        attrs = DataFrame({"A": ["color: red; foo: bar", "color: blue; foo: baz"]})
        self.styler._update_ctx(attrs)
        expected = {(0, 0): ["color: red", " foo: bar"], (1, 0): ["color: blue", " foo: baz"]}
        self.assertEqual(self.styler.ctx, expected)

    def test_update_ctx_flatten_multi_traliing_semi(self):
        attrs = DataFrame({"A": ["color: red; foo: bar;", "color: blue; foo: baz;"]})
        self.styler._update_ctx(attrs)
        expected = {(0, 0): ["color: red", " foo: bar"], (1, 0): ["color: blue", " foo: baz"]}
        self.assertEqual(self.styler.ctx, expected)

    def test_copy(self):
        s2 = copy.copy(self.styler)
        self.assertTrue(self.styler is not s2)
        self.assertTrue(self.styler.ctx is s2.ctx)  # shallow
        self.assertTrue(self.styler._todo is s2._todo)

        self.styler._update_ctx(self.attrs)
        self.styler.highlight_max()
        self.assertEqual(self.styler.ctx, s2.ctx)
        self.assertEqual(self.styler._todo, s2._todo)

    def test_deepcopy(self):
        s2 = copy.deepcopy(self.styler)
        self.assertTrue(self.styler is not s2)
        self.assertTrue(self.styler.ctx is not s2.ctx)
        self.assertTrue(self.styler._todo is not s2._todo)

        self.styler._update_ctx(self.attrs)
        self.styler.highlight_max()
        self.assertNotEqual(self.styler.ctx, s2.ctx)
        self.assertEqual(s2._todo, [])
        self.assertNotEqual(self.styler._todo, s2._todo)

    def test_clear(self):
        s = self.df.style.highlight_max()._compute()
        self.assertTrue(len(s.ctx) > 0)
        self.assertTrue(len(s._todo) > 0)
        s.clear()
        self.assertTrue(len(s.ctx) == 0)
        self.assertTrue(len(s._todo) == 0)

    def test_render(self):
        df = pd.DataFrame({"A": [0, 1]})
        style = lambda x: pd.Series(["color: red", "color: blue"], name=x.name)
        s = Styler(df, uuid="AB").apply(style)
        s.render()
        # it worked?

    def test_render_double(self):
        df = pd.DataFrame({"A": [0, 1]})
        style = lambda x: pd.Series(["color: red; border: 1px", "color: blue; border: 2px"], name=x.name)
        s = Styler(df, uuid="AB").apply(style)
        s.render()
        # it worked?

    def test_set_properties(self):
        df = pd.DataFrame({"A": [0, 1]})
        result = df.style.set_properties(color="white", size="10px")._compute().ctx
        # order is deterministic
        v = ["color: white", "size: 10px"]
        expected = {(0, 0): v, (1, 0): v}
        self.assertEqual(result.keys(), expected.keys())
        for v1, v2 in zip(result.values(), expected.values()):
            self.assertEqual(sorted(v1), sorted(v2))

    def test_set_properties_subset(self):
#.........這裏部分代碼省略.........
開發者ID:ChristopherShort,項目名稱:pandas,代碼行數:101,代碼來源:test_style.py

示例12: test_render

 def test_render(self):
     df = pd.DataFrame({"A": [0, 1]})
     style = lambda x: pd.Series(["color: red", "color: blue"], name=x.name)
     s = Styler(df, uuid="AB").apply(style)
     s.render()
開發者ID:ChristopherShort,項目名稱:pandas,代碼行數:5,代碼來源:test_style.py


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