Skip to content

Commit d78f198

Browse files
committed
ENH: changes requested in issue
1 parent 92ae0ab commit d78f198

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
@@ -46,11 +46,6 @@ Bug fixes
4646
- Bug in ``.groupby(..).rolling(..)`` where passing ``closed`` with column selection would raise a ``ValueError`` (:issue:`35549`)
4747
- Bug in :class:`DataFrame` constructor failing to raise ``ValueError`` in some cases when data and index have mismatched lengths (:issue:`33437`)
4848

49-
Other enhancements
50-
~~~~~~~~~~~~~~~~~~
51-
52-
- :meth:`Styler.set_table_styles` now allows the direct styling of rows and columns and can be chained (:issue:`35607`)
53-
5449
.. ---------------------------------------------------------------------------
5550
5651
.. _whatsnew_111.contributors:

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ For example:
5151
Other enhancements
5252
^^^^^^^^^^^^^^^^^^
5353
- :class:`Index` with object dtype supports division and multiplication (:issue:`34160`)
54+
- :meth:`Styler.set_table_styles` now allows the direct styling of rows and columns and can be chained (:issue:`35607`)
5455
-
5556
-
5657

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
@@ -1692,6 +1692,7 @@ def test_no_cell_ids(self):
16921692
assert s.find('<td class="data row0 col0" >') != -1
16931693

16941694
def test_chaining_table_styles(self):
1695+
# GH 35607
16951696
df = pd.DataFrame(data=[[0, 1], [1, 2]], columns=["A", "B"])
16961697
styler = df.style.set_table_styles(
16971698
[{"selector": "", "props": [("background-color", "yellow")]}]
@@ -1702,6 +1703,7 @@ def test_chaining_table_styles(self):
17021703
assert len(styler.table_styles) == 2
17031704

17041705
def test_column_and_row_styling(self):
1706+
# GH 35607
17051707
df = pd.DataFrame(data=[[0, 1], [1, 2]], columns=["A", "B"])
17061708
s = Styler(df, uuid="_")
17071709
s = s.set_table_styles({"A": [{"selector": "", "props": [("color", "blue")]}]})

0 commit comments

Comments
 (0)