Skip to content
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

ENH: Styler.apply_index and Styler.applymap_index for conditional formatting of column/index headers #41893

Merged
merged 31 commits into from
Aug 11, 2021

Conversation

attack68
Copy link
Contributor

@attack68 attack68 commented Jun 9, 2021

Checks an item on #41648 and #41693

Currently Styler has the apply and applymap methods to perform conditional styling on the data elements.

Recent additions to Styler.set_table_styles allows styling of index and column headers if the key-labels are directly specified. But this solution cannot work with LaTeX, nor excel output, and is obviously not conditionally applied.

This PR adds the mechanics to perform conditional styling on per level index labels

Follow-ons are:

Screen Shot 2021-06-13 at 10 32 40

@attack68 attack68 changed the title ENH: Styler apply and applymap over the index instead of the data [WIP] ENH: Styler apply and applymap over the index instead of the data Jun 9, 2021
@attack68 attack68 marked this pull request as draft June 12, 2021 07:47
@attack68 attack68 added Styler conditional formatting using DataFrame.style Enhancement labels Jun 12, 2021
@attack68 attack68 marked this pull request as ready for review June 14, 2021 07:48
@attack68 attack68 changed the title [WIP] ENH: Styler apply and applymap over the index instead of the data ENH: Styler apply and applymap over the index instead of the data Jun 14, 2021
@attack68 attack68 changed the title ENH: Styler apply and applymap over the index instead of the data ENH: Styler.apply_header and Styler.applymap_header for conditional formatting of column/index headers Jun 14, 2021
…x_specific

# Conflicts:
#	doc/source/reference/style.rst
#	pandas/io/formats/style_render.py
#	pandas/tests/io/formats/style/test_html.py
…x_specific

# Conflicts:
#	pandas/tests/io/formats/style/test_style.py
…x_specific

# Conflicts:
#	doc/source/whatsnew/v1.4.0.rst
#	pandas/tests/io/formats/style/test_style.py
@attack68 attack68 added this to the 1.4 milestone Jul 27, 2021
Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't apply_axis be more appropriate? e.g. don't see _header anywhere else in the codebase

do we need both (i get that we have symmetry, but is this overkill)?

@attack68
Copy link
Contributor Author

wouldn't apply_axis be more appropriate? e.g. don't see _header anywhere else in the codebase

do we need both (i get that we have symmetry, but is this overkill)?

i came up with 'header' becuase in the HTML that styler renders the index and column header cells are <th> (table header) elements, and these are the ones targeted by this method.

I'm not tied to the name but apply_axis feels a bit odd, since axis is already used by pandas for a common meaning and not really to do with impacting the values of the index or column headers.

apply_index(axis="columns") might be an alternative to make it clear that the function is being applied to either the index values or the column header values??

…x_specific

# Conflicts:
#	doc/source/whatsnew/v1.4.0.rst
#	pandas/tests/io/formats/style/test_html.py
…x_specific

# Conflicts:
#	doc/source/whatsnew/v1.4.0.rst
#	pandas/io/formats/style_render.py
#	pandas/tests/io/formats/style/test_style.py
…x_specific

# Conflicts:
#	pandas/tests/io/formats/style/test_style.py
@attack68 attack68 changed the title ENH: Styler.apply_header and Styler.applymap_header for conditional formatting of column/index headers ENH: Styler.apply_index and Styler.applymap_index for conditional formatting of column/index headers Aug 9, 2021
@@ -38,6 +38,7 @@ Other enhancements
- :meth:`Series.ewm`, :meth:`DataFrame.ewm`, now support a ``method`` argument with a ``'table'`` option that performs the windowing operation over an entire :class:`DataFrame`. See :ref:`Window Overview <window.overview>` for performance and functional benefits (:issue:`42273`)
- Added ``sparse_index`` and ``sparse_columns`` keyword arguments to :meth:`.Styler.to_html` (:issue:`41946`)
- Added keyword argument ``environment`` to :meth:`.Styler.to_latex` also allowing a specific "longtable" entry with a separate jinja2 template (:issue:`41866`)
- :meth:`.Styler.apply_index` and :meth:`.Styler.applymap_index` added to allow conditional styling of index and column header values (:issue:`41893`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be worth creating a styler sub-section in other enhancements (followon)

@jreback jreback merged commit 5bd9bac into pandas-dev:master Aug 11, 2021
@jreback
Copy link
Contributor

jreback commented Aug 11, 2021

thanks @attack68 very nice!

@attack68 attack68 deleted the styler_apply_index_specific branch August 12, 2021 07:30
feefladder pushed a commit to feefladder/pandas that referenced this pull request Sep 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Styler conditional formatting using DataFrame.style
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants