Skip to content

Commit a042654

Browse files
committed
ENH: changes requested in issue
1 parent 82db42f commit a042654

File tree

4 files changed

+31
-20
lines changed

4 files changed

+31
-20
lines changed

doc/source/whatsnew/v1.1.1.rst

-5
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,6 @@ Categorical
5151

5252
-
5353

54-
Other enhancements
55-
~~~~~~~~~~~~~~~~~~
56-
57-
- :meth:`Styler.set_table_styles` now allows the direct styling of rows and columns and can be chained (:issue:`35607`)
58-
5954
.. ---------------------------------------------------------------------------
6055
6156
.. _whatsnew_111.contributors:

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Enhancements
1818
Other enhancements
1919
^^^^^^^^^^^^^^^^^^
2020

21+
- :meth:`Styler.set_table_styles` now allows the direct styling of rows and columns and can be chained (:issue:`35607`)
2122
-
2223
-
2324

pandas/io/formats/style.py

+28-15
Original file line numberDiff line numberDiff line change
@@ -933,15 +933,23 @@ def set_table_styles(self, table_styles, axis=0, overwrite=True) -> "Styler":
933933
in their respective tuple form. The dict values should be
934934
a list as specified in the form with CSS selectors and
935935
props that will be applied to the specified row or column.
936+
937+
.. versionchanged:: 1.2.0
938+
936939
axis : {0 or 'index', 1 or 'columns', None}, default 0
937940
Apply to each column (``axis=0`` or ``'index'``), to each row
938941
(``axis=1`` or ``'columns'``). Only used if `table_styles` is
939942
dict.
943+
944+
.. versionadded:: 1.2.0
945+
940946
overwrite : boolean, default True
941947
Styles are replaced if `True`, or extended if `False`. CSS
942948
rules are preserved so most recent styles set will dominate
943949
if selectors intersect.
944950
951+
.. versionadded:: 1.2.0
952+
945953
Returns
946954
-------
947955
self : Styler
@@ -954,33 +962,38 @@ def set_table_styles(self, table_styles, axis=0, overwrite=True) -> "Styler":
954962
... [{'selector': 'tr:hover',
955963
... 'props': [('background-color', 'yellow')]}]
956964
... )
965+
966+
Adding column styling by name
967+
957968
>>> df.style.set_table_styles({
958969
... 'A': [{'selector': '',
959970
... 'props': [('color', 'red')]}],
960971
... 'B': [{'selector': 'td',
961972
... 'props': [('color', 'blue')]}]
962973
... }, overwrite=False)
974+
975+
Adding row styling
976+
963977
>>> df.style.set_table_styles({
964978
... 0: [{'selector': 'td:hover',
965979
... 'props': [('font-size', '25px')]}]
966980
... }, axis=1, overwrite=False)
967981
"""
968-
if isinstance(table_styles, dict):
969-
if axis == 0 or axis == "index":
970-
obj = self.data.columns
971-
idf = ".col"
982+
if is_dict_like(table_styles):
983+
if axis in [0, "index"]:
984+
obj, idf = self.data.columns, ".col"
972985
else:
973-
obj = self.data.index
974-
idf = ".row"
975-
976-
_styles = []
977-
for key, styles in table_styles.items():
978-
for s in styles:
979-
c = str(obj.get_loc(key))
980-
_styles.append(
981-
{"selector": s["selector"] + idf + c, "props": s["props"]}
982-
)
983-
table_styles = _styles
986+
obj, idf = self.data.index, ".row"
987+
988+
table_styles = [
989+
{
990+
"selector": s["selector"] + idf + str(obj.get_loc(key)),
991+
"props": s["props"],
992+
}
993+
for key, styles in table_styles.items()
994+
for s in styles
995+
]
996+
984997
if not overwrite and self.table_styles is not None:
985998
self.table_styles.extend(table_styles)
986999
else:

pandas/tests/io/formats/test_style.py

+2
Original file line numberDiff line numberDiff line change
@@ -1683,6 +1683,7 @@ def f(a, b, styler):
16831683
assert result == (1, 2, styler)
16841684

16851685
def test_chaining_table_styles(self):
1686+
# GH 35607
16861687
df = pd.DataFrame(data=[[0, 1], [1, 2]], columns=["A", "B"])
16871688
styler = df.style.set_table_styles(
16881689
[{"selector": "", "props": [("background-color", "yellow")]}]
@@ -1693,6 +1694,7 @@ def test_chaining_table_styles(self):
16931694
assert len(styler.table_styles) == 2
16941695

16951696
def test_column_and_row_styling(self):
1697+
# GH 35607
16961698
df = pd.DataFrame(data=[[0, 1], [1, 2]], columns=["A", "B"])
16971699
s = Styler(df, uuid="_")
16981700
s = s.set_table_styles({"A": [{"selector": "", "props": [("color", "blue")]}]})

0 commit comments

Comments
 (0)