Skip to content

BUG: Fix Empty DataFrame.style.render raises IndexError #15971

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
wants to merge 1 commit into from

Conversation

sandipchatterjee
Copy link

Fix attempt for Issue #15953

Handles DataFrames and Series with no rows or columns
using pd.DataFrame().empty or pd.Series().empty

In [1]: import pandas as pd

In [2]: pd.DataFrame().style.render()
Out[2]: '\n        <style  type="text/css" >\n        \n        \n        </style>\n\n        <table id="T_de3b3e8c_1e30_11e7_9abb_a8667f1b25b4" None>\n        \n\n        <thead>\n            \n        </thead>\n        <tbody>\n            \n        </tbody>\n        </table>\n        '

Fix attempt for Issue pandas-dev#15953

Handles DataFrames and Series with no rows or columns
using `pd.DataFrame().empty` or `pd.Series().empty`
Copy link
Contributor

@TomAugspurger TomAugspurger left a comment

Choose a reason for hiding this comment

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

Thanks! One comment on the fix. Could you also add tests and a release note in doc/source/whatsnew/v0.20.0.txt?

@@ -218,8 +218,8 @@ def format_attr(pair):

cell_context = dict()

n_rlvls = self.data.index.nlevels
n_clvls = self.data.columns.nlevels
n_rlvls = 0 if self.data.empty else self.data.index.nlevels
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you'll have to change this around a bit. DataFrame.empty is True if either the index or columns is empty. So you get

pd.DataFrame([], index=[1, 2]).empty  #  True
pd.DataFrame([], columns=['A', 'B']).empty  # True

@TomAugspurger
Copy link
Contributor

I think our desired output is the same as the notebook

screen shot 2017-04-10 at 4 25 09 pm

@codecov
Copy link

codecov bot commented Apr 10, 2017

Codecov Report

Merging #15971 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #15971      +/-   ##
==========================================
+ Coverage      91%   91.01%   +<.01%     
==========================================
  Files         145      145              
  Lines       49581    49581              
==========================================
+ Hits        45122    45124       +2     
+ Misses       4459     4457       -2
Flag Coverage Δ
#multiple 88.77% <100%> (ø) ⬆️
#single 40.57% <0%> (ø) ⬆️
Impacted Files Coverage Δ
pandas/formats/style.py 96.17% <100%> (ø) ⬆️
pandas/util/testing.py 81.13% <0%> (+0.18%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 838e09c...5f88c22. Read the comment docs.

@jreback jreback added Bug IO HTML read_html, to_html, Styler.apply, Styler.applymap labels Apr 11, 2017
@jreback
Copy link
Contributor

jreback commented May 7, 2017

pls rebase / update according to the comments. Needs a tests.

@TomAugspurger
Copy link
Contributor

TomAugspurger commented May 8, 2017

whatsnew notes should go in whatsnew/0.20.2.txt now.

@TomAugspurger TomAugspurger added this to the 0.20.2 milestone May 8, 2017
@TomAugspurger
Copy link
Contributor

Looks like #16441 fixed this in the meantime. Thanks anyways @sandipchatterjee, sorry about the duplicated effort.

@TomAugspurger TomAugspurger modified the milestones: No action, 0.20.2 May 30, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO HTML read_html, to_html, Styler.apply, Styler.applymap
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: Empty DataFrame.style.render raises IndexError
3 participants