Skip to content

Commit cc058e0

Browse files
authored
Bug multiple css selectors GH44011 (#44023)
1 parent dca6901 commit cc058e0

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

doc/source/whatsnew/v1.4.0.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,8 @@ Styler
569569
- Bug when rendering an empty DataFrame with a named index (:issue:`43305`).
570570
- Bug when rendering a single level MultiIndex (:issue:`43383`).
571571
- Bug when combining non-sparse rendering and :meth:`.Styler.hide_columns` or :meth:`.Styler.hide_index` (:issue:`43464`)
572+
- Bug setting a table style when using multiple selectors in :class:`.Styler` (:issue:`44011`)
573+
-
572574

573575
Other
574576
^^^^^

pandas/io/formats/style.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
StylerRenderer,
5454
Subset,
5555
Tooltips,
56+
format_table_styles,
5657
maybe_convert_css_to_tuples,
5758
non_reducing_slice,
5859
refactor_levels,
@@ -2047,7 +2048,7 @@ def set_table_styles(
20472048
}
20482049
for key, styles in table_styles.items()
20492050
for idx in obj.get_indexer_for([key])
2050-
for s in styles
2051+
for s in format_table_styles(styles)
20512052
]
20522053
else:
20532054
table_styles = [

pandas/io/formats/style_render.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def _translate(
226226
# construct render dict
227227
d = {
228228
"uuid": self.uuid,
229-
"table_styles": _format_table_styles(self.table_styles or []),
229+
"table_styles": format_table_styles(self.table_styles or []),
230230
"caption": self.caption,
231231
}
232232

@@ -1173,7 +1173,7 @@ def _is_visible(idx_row, idx_col, lengths) -> bool:
11731173
return (idx_col, idx_row) in lengths
11741174

11751175

1176-
def _format_table_styles(styles: CSSStyles) -> CSSStyles:
1176+
def format_table_styles(styles: CSSStyles) -> CSSStyles:
11771177
"""
11781178
looks for multiple CSS selectors and separates them:
11791179
[{'selector': 'td, th', 'props': 'a:v;'}]

pandas/tests/io/formats/style/test_style.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,19 @@ def test_table_styles_multiple(self):
829829
{"selector": "tr", "props": [("color", "green")]},
830830
]
831831

832+
def test_table_styles_dict_multiple_selectors(self):
833+
# GH 44011
834+
result = self.df.style.set_table_styles(
835+
[{"selector": "th,td", "props": [("border-left", "2px solid black")]}]
836+
)._translate(True, True)["table_styles"]
837+
838+
expected = [
839+
{"selector": "th", "props": [("border-left", "2px solid black")]},
840+
{"selector": "td", "props": [("border-left", "2px solid black")]},
841+
]
842+
843+
assert result == expected
844+
832845
def test_maybe_convert_css_to_tuples(self):
833846
expected = [("a", "b"), ("c", "d e")]
834847
assert maybe_convert_css_to_tuples("a:b;c:d e;") == expected

0 commit comments

Comments
 (0)