Skip to content

Fix styling of DataFrame for columns with boolean label #47848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,7 @@ Styler
- Bug in :meth:`Styler.set_sticky` leading to white text on white background in dark mode (:issue:`46984`)
- Bug in :meth:`Styler.to_latex` causing ``UnboundLocalError`` when ``clines="all;data"`` and the ``DataFrame`` has no rows. (:issue:`47203`)
- Bug in :meth:`Styler.to_excel` when using ``vertical-align: middle;`` with ``xlsxwriter`` engine (:issue:`30107`)
- Bug when applying styles to a DataFrame with boolean column labels (:issue:`47838`)

Metadata
^^^^^^^^
Expand Down
12 changes: 9 additions & 3 deletions pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -2977,6 +2977,12 @@ def hide(
# A collection of "builtin" styles
# -----------------------------------------------------------------------

def _get_numeric_subset_default(self):
# Returns a boolean mask indicating where `self.data` has numerical columns.
# Choosing a mask as opposed to the column names also works for
# boolean column labels (GH47838).
return self.data.columns.isin(self.data.select_dtypes(include=np.number))

@doc(
name="background",
alt="text",
Expand Down Expand Up @@ -3120,7 +3126,7 @@ def background_gradient(
.. figure:: ../../_static/style/{image_prefix}_axNone_gmap.png
"""
if subset is None and gmap is None:
subset = self.data.select_dtypes(include=np.number).columns
subset = self._get_numeric_subset_default()

self.apply(
_background_gradient,
Expand Down Expand Up @@ -3155,7 +3161,7 @@ def text_gradient(
gmap: Sequence | None = None,
) -> Styler:
if subset is None and gmap is None:
subset = self.data.select_dtypes(include=np.number).columns
subset = self._get_numeric_subset_default()

return self.apply(
_background_gradient,
Expand Down Expand Up @@ -3308,7 +3314,7 @@ def bar(
raise ValueError(f"`height` must be a value in [0, 100], got {height}")

if subset is None:
subset = self.data.select_dtypes(include=np.number).columns
subset = self._get_numeric_subset_default()

self.apply(
_bar,
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/io/formats/style/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,22 @@ def color_negative_red(val):
df.loc[pct_subset]
df.style.applymap(color_negative_red, subset=pct_subset)

@pytest.mark.parametrize(
"stylefunc", ["background_gradient", "bar", "text_gradient"]
)
def test_subset_for_boolean_cols(self, stylefunc):
# GH47838
df = DataFrame(
[
[1, 2],
[3, 4],
],
columns=[False, True],
)
styled = getattr(df.style, stylefunc)()
styled._compute()
assert set(styled.ctx) == {(0, 0), (0, 1), (1, 0), (1, 1)}

def test_empty(self):
df = DataFrame({"A": [1, 0]})
s = df.style
Expand Down