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

Show Index Headers on DataFrames with Style #12090

Merged
merged 1 commit into from
Jan 20, 2016

Conversation

HHammond
Copy link
Contributor

Partial solution for #11655 (Conditional HTML styling hides MultiIndex structure) and #11610 (Followup to Conditional HTML Styling).

The Style API is inconsistent with DataFrame.to_html() when printing index names. Currently the style API doesn't print any index names but to_html and the standard notebook repr functions do.

This PR adds a row for index headings in the Styler._translate method. This PR does not cause multi-index rownames to span multiple rows, but I can work on adding that if people want.

Reproducing Output:

import pandas as pd
import numpy as np

np.random.seed(24)
df = pd.DataFrame({'A': np.linspace(1, 10, 10)})
df = pd.concat([df, pd.DataFrame(np.random.randn(10, 4),
                                 columns=list('BCDE'))],
               axis=1)
df.set_index(['A', 'B']).style
Current Output

screen shot 2016-01-19 at 10 19 08 am

##### Patched Output:

screen shot 2016-01-19 at 10 19 19 am

## TODO: - [X] Add Tests

@@ -206,6 +206,22 @@ def _translate(self):
"class": " ".join(cs)})
head.append(row_es)

if self.data.index.names:
index_header = []
Copy link
Contributor Author

Choose a reason for hiding this comment

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

possibly rename to index_row for clarity.

@jreback
Copy link
Contributor

jreback commented Jan 19, 2016

tests pls

@jreback jreback added Output-Formatting __repr__ of pandas objects, to_string IO HTML read_html, to_html, Styler.apply, Styler.applymap MultiIndex labels Jan 19, 2016
@HHammond
Copy link
Contributor Author

@jreback tests added. Do you think I need to add more tests?

@TomAugspurger
Copy link
Contributor

Just gave a quick look, and the changes look good to me. Could you add a release note in doc/source/whatsnew/v0.18.0.txt under the bug fixes section?

Slightly off topic, but I saw your PrettyPandas the other day, looks great! Feel free to bug me with anything that's causing you difficulty with developing that. I've been busy with other types of work, so I haven't had time to actually use .style yet. e.g. I think pandas could have a decorator that makes it easier to hook into our lazy style evaluation so that you can export the styles more easily.

@@ -130,6 +130,38 @@ def test_set_properties_subset(self):
expected = {(0, 0): ['color: white']}
self.assertEqual(result, expected)

def test_index_name(self):
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4], 'C': [5, 6]})
Copy link
Contributor

Choose a reason for hiding this comment

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

add the issue number as a comment here (and below)

@jreback
Copy link
Contributor

jreback commented Jan 19, 2016

@HHammond some small comments. otherwise looks good.

@jreback
Copy link
Contributor

jreback commented Jan 19, 2016

can you run git diff master | flake8 --diff on this as well (and if anything comes up, pls fix). we are trying to fix PEP across the code base.

@HHammond HHammond force-pushed the master branch 2 times, most recently from d54cb4c to 47cfe4f Compare January 19, 2016 17:14
@HHammond
Copy link
Contributor Author

@TomAugspurger Thanks, I'm glad you like PrettyPandas. I'll try and explore the limits of what style can do and keep you in the loop if anything comes up.

It's been added to the what's new page, I've added the issue to the tests, and flake8 is happy with my patch. Anything else you guys need?

@HHammond
Copy link
Contributor Author

@jreback is this good to merge?

@jreback jreback added this to the 0.18.0 milestone Jan 20, 2016
@jreback
Copy link
Contributor

jreback commented Jan 20, 2016

@jreback
Copy link
Contributor

jreback commented Jan 20, 2016

@TomAugspurger ball in your court

@HHammond
Copy link
Contributor Author

Squashed

TomAugspurger pushed a commit that referenced this pull request Jan 20, 2016
Show Index Headers on DataFrames with Style
@TomAugspurger TomAugspurger merged commit a783d1d into pandas-dev:master Jan 20, 2016
@TomAugspurger
Copy link
Contributor

@HHammond thanks!

@wesm
Copy link
Member

wesm commented Jan 24, 2016

Please use the patch tool next time!

@TomAugspurger
Copy link
Contributor

Sorry, I remembered that seconds after merging, old habits :)

@wesm
Copy link
Member

wesm commented Jan 24, 2016

No problem, but there's no need to ask people to squash any more either if you use that tool (unless there is some messy commit history or merge commits, of course).

@sinhrks
Copy link
Member

sinhrks commented Feb 6, 2016

Still mismatches with normal to_html which skips if index name is None.

2016-02-06 14 28 00

@HHammond Appreciated if you can work.

@HHammond
Copy link
Contributor Author

HHammond commented Feb 6, 2016

I can take a look at that on Sunday or Monday.

@HHammond
Copy link
Contributor Author

HHammond commented Feb 7, 2016

@TomAugspurger I have a patch for @sinhrks request, is it something that belongs in #12162 or would you prefer I create a new PR for that?

Patch:
diff --git a/pandas/core/style.py b/pandas/core/style.py
index a5a42c2..45e9dfc 100644
--- a/pandas/core/style.py
+++ b/pandas/core/style.py
@@ -206,7 +206,7 @@ class Styler(object):
                                "class": " ".join(cs)})
             head.append(row_es)

-        if self.data.index.names:
+        if self.data.index.names and self.data.index.names != [None]:
             index_header_row = []

             for c, name in enumerate(self.data.index.names):

@TomAugspurger
Copy link
Contributor

You can do it as a separate PR if you have a chance. You might want to base it off #12162 to avoid merge conflicts.

@HHammond
Copy link
Contributor Author

HHammond commented Feb 8, 2016

PR: #12260

TomAugspurger added a commit that referenced this pull request Mar 3, 2016
Addresses
#12090 (comment) by
making the `Styler` behaviour match regular `to_html` behaviour.
This PR is based from #12162.    ##### New behaviour  <img width="596"
alt="screen shot 2016-02-08 at 10 35 23 am"
src="https://cloud.githubusercontent.com/assets/3064019/12890011
/b183e81e-ce4f-11e5-9b9f-c021fcb33c5a.png">    cc @TomAugspurger

Author: Tom Augspurger <tom.w.augspurger@gmail.com>
Author: Henry Hammond <henryhhammond92@gmail.com>

Closes #12260 from HHammond/style-remove-col-index-none and squashes the following commits:

48c2f4c [Henry Hammond] BUG: Ignore style column headers when None
a15248a [Tom Augspurger] ENH: display_format for style
@attack68 attack68 added the Styler conditional formatting using DataFrame.style label Mar 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO HTML read_html, to_html, Styler.apply, Styler.applymap MultiIndex Output-Formatting __repr__ of pandas objects, to_string Styler conditional formatting using DataFrame.style
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants