Skip to content

ENH: Styler.format_index() to display index values similarly to data-values with format() #43101

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 38 commits into from
Sep 16, 2021

Conversation

attack68
Copy link
Contributor

@attack68 attack68 commented Aug 18, 2021

  • adds features and method with docstrings
  • update user guide
  • whats new
  • add tests

This PR adds the method format_index so that the typical format routines can also be applied to display the index labels and columns headers of a Styler.

With all the recent functionality here is a complete example:

Screen Shot 2021-08-18 at 22 55 02

Notes

Also had to move _refactor_levels function from style.py to style_render.py for use here.

@attack68 attack68 changed the title Styler format index ENH: Styler.format_index() to display index values similarly to data-values with format() Aug 18, 2021
@jreback jreback added the Styler conditional formatting using DataFrame.style label Aug 19, 2021
@jreback
Copy link
Contributor

jreback commented Aug 19, 2021

hmm why do we want to add methods like this? e.g. if you want to string format an index you can already

@attack68
Copy link
Contributor Author

attack68 commented Aug 20, 2021

  • Styler displays values without altering the underlying DataFrame. Using pd.Index.format returns a formatted Index which you need to overwrite as the DataFrames Index prior to calling Styler, therefore it breaks the typical usage pattern.
  • pd.Index.format does not have the additional options available in format_index like HTML/LaTeX escaping, and has a different signature to Styler.format which might confuse users.
  • Needing to overwrite the Index with your display format means you can lose functionality of indexing. Here is an example of how this helps preserve functionality:

Screen Shot 2021-08-20 at 09 01 21

Do throw this out to the team for feedback...

@attack68 attack68 changed the title ENH: Styler.format_index() to display index values similarly to data-values with format() WIP ENH: Styler.format_index() to display index values similarly to data-values with format() Aug 20, 2021
@attack68
Copy link
Contributor Author

attack68 commented Aug 30, 2021

User Guide Updates:

Screen Shot 2021-08-31 at 13 02 29

@attack68 attack68 changed the title WIP ENH: Styler.format_index() to display index values similarly to data-values with format() ENH: Styler.format_index() to display index values similarly to data-values with format() Aug 30, 2021
@attack68 attack68 marked this pull request as ready for review August 30, 2021 18:50
----------
formatter : str, callable, dict or None
Object to define how values are displayed. See notes.
axis : int, str
Copy link
Contributor

Choose a reason for hiding this comment

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

give the allowed (e.g. 0,1,index,columns)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@jreback jreback added this to the 1.4 milestone Sep 6, 2021
@attack68 attack68 force-pushed the styler_format_index branch from cf60e44 to 06ce6bf Compare September 8, 2021 18:01
# Conflicts:
#	pandas/io/formats/style_render.py
@attack68
Copy link
Contributor Author

attack68 commented Sep 9, 2021

these tests consistently fail running the ipython in source/user_guide/scale.rst dealing with dask DataFrame.
But this PR is unrelated to that so I don't know the reason. Couldn't really find other PRs suffering from the same test failures?
The docs build locally without errors so its puzzling.

@jreback
Copy link
Contributor

jreback commented Sep 9, 2021

these tests consistently fail running the ipython in source/user_guide/scale.rst dealing with dask DataFrame.
But this PR is unrelated to that so I don't know the reason. Couldn't really find other PRs suffering from the same test failures?
The docs build locally without errors so its puzzling.

hmm, did dask version change?

@jreback
Copy link
Contributor

jreback commented Sep 10, 2021

@attack68
Copy link
Contributor Author

looks like some actual doc failures: https://github.com/pandas-dev/pandas/pull/43101/checks?check_run_id=3570623205

everytime says build completed with 8 warnings, and every warning is in respect to /home/runner/work/pandas/pandas/doc/source/user_guide/scale.rst, which is an unaltered file, and never calls Styler or the impacted files here.

The docs build locally without warning so I am puzzled, will see if I can come up with anything

# Conflicts:
#	pandas/io/formats/style_render.py
@attack68 attack68 merged commit 4081fc7 into pandas-dev:master Sep 16, 2021
@attack68
Copy link
Contributor Author

@jreback managed to get this to pass after merge. revert if necessary but I think should be ok now.

@jreback
Copy link
Contributor

jreback commented Sep 16, 2021

ok great. very odd that that was happening,

@attack68 attack68 deleted the styler_format_index branch September 20, 2021 22:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Styler conditional formatting using DataFrame.style
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants