Skip to content

Export styler doesn't seem to include hide_index() #22851

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

Closed
aaronbarzilai opened this issue Sep 27, 2018 · 5 comments
Closed

Export styler doesn't seem to include hide_index() #22851

aaronbarzilai opened this issue Sep 27, 2018 · 5 comments
Labels
Bug good first issue IO HTML read_html, to_html, Styler.apply, Styler.applymap Output-Formatting __repr__ of pandas objects, to_string Styler conditional formatting using DataFrame.style

Comments

@aaronbarzilai
Copy link

Code Sample, a copy-pastable example if possible

# Code based on example in docs
def color_negative_red(val):
    """
    Takes a scalar and returns a string with
    the css property `'color: red'` for negative
    strings, black otherwise.
    """
    color = 'red' if val < 0 else 'black'
    return 'color: %s' % color

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.iloc[0, 2] = np.nan

display(df.style)

style1 = df.style.applymap(color_negative_red).hide_index() #option 1 when testing
#style1 = df.style.hide_index() #option 2 when testing

display(style1)

thestyle = style1.export()
print(thestyle)

df2 = -df
df2

style2 = df2.style
style2.use(style1.export())

Problem description

I'm trying to reuse a style for a second table, following the model in the docs at https://pandas.pydata.org/pandas-docs/stable/style.html#Sharing-Styles

I find that when I use my "option 1" above with red and hide index, the output for df styles as I would hope.

image

Note that the hide index seems empty when I print the styler, but it seems to know a second style is applied through the chaining.

When I then show df2, it has exported the red, but not the hide index.

image

If I repeat with "Option 2", the index is hidden for df but not df2

I think I'm doing this correctly given the docs, please let me know if I'm not calling it correctly or it is known that I can't export chained styles. If it's in the latter, is that in the documentation? I didn't see it.

Thanks, really appreciate all the effort on pandas,
Aaron

Note: We receive a lot of issues on our GitHub tracker, so it is very possible that your issue has been posted before. Please check first before submitting so that we do not have to handle and close duplicates!

I tried to check by searching issues, sorry if I missed it

Note: Many problems can be resolved by simply upgrading pandas to the latest version. Before submitting, please check if that solution works for you. If possible, you may want to check if master addresses this issue, but that is not necessary.

I just did, see show_versions output

For documentation-related issues, you can check the latest versions of the docs on master here:

https://pandas-docs.github.io/pandas-docs-travis/

If the issue has not been resolved there, go ahead and file it in the issue tracker.

Expected Output

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.5.final.0
python-bits: 64
OS: Linux
OS-release: 4.15.0-34-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.23.4
pytest: None
pip: 9.0.3
setuptools: 39.0.1
Cython: None
numpy: 1.14.3
scipy: None
pyarrow: None
xarray: None
IPython: 6.3.1
sphinx: None
patsy: None
dateutil: 2.7.2
pytz: 2018.4
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.2.2
openpyxl: None
xlrd: 1.1.0
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 1.0.1
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@TomAugspurger
Copy link
Contributor

Styler.export / Styler.use don't take into account attributes like self.hidden_index , precision, etc. Basically anything that can be set in the constructor or one of the set_* (or I guess hide_*) methods. Some of these probably should be exported, others (like uuid?) maybe shouldn't.

So going forward, I would say

  1. figure out which attributes make sense to export
  2. Add a parameter to Styler.export to control whether attributes should be exported (False by default, for backwards compatibility)
  3. Update Styler.export to
    • copy self.todo
    • add additional tasks for setting things like hidden index, (e.g., lambda x: x.hide_index if hidden_index is True)

@TomAugspurger TomAugspurger added Output-Formatting __repr__ of pandas objects, to_string IO HTML read_html, to_html, Styler.apply, Styler.applymap labels Sep 27, 2018
@TomAugspurger TomAugspurger added this to the Contributions Welcome milestone Sep 27, 2018
@aaronbarzilai
Copy link
Author

aaronbarzilai commented Sep 27, 2018 via email

@TomAugspurger
Copy link
Contributor

The workaround for now is

if style1.hidden_index:
    style2 = style2.hide_index()

@mroeschke mroeschke added the Bug label May 7, 2020
@attack68 attack68 added the Styler conditional formatting using DataFrame.style label Feb 20, 2021
@attack68
Copy link
Contributor

linked to #40484 since simialr discussion/logic should probably be applied to what copy clear and export does.

@attack68
Copy link
Contributor

Im going to link this to #40675 and then close it so that thread can itemise this issue as well as keep track of similars

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug good first issue IO HTML read_html, to_html, Styler.apply, Styler.applymap Output-Formatting __repr__ of pandas objects, to_string Styler conditional formatting using DataFrame.style
Projects
None yet
Development

No branches or pull requests

4 participants