Skip to content

to_html function fail when max_rows is specified #8273

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
pilollipietro opened this issue Sep 15, 2014 · 4 comments
Closed

to_html function fail when max_rows is specified #8273

pilollipietro opened this issue Sep 15, 2014 · 4 comments
Labels
IO HTML read_html, to_html, Styler.apply, Styler.applymap Output-Formatting __repr__ of pandas objects, to_string

Comments

@pilollipietro
Copy link

The error is:
list index out of range
Exception Location: /home/pietro/lod4statenv/local/lib/python2.7/site-packages/pandas/core/format.py in _write_body, line 923

In order to fix it I have replaced line 922 from:

for i in range(len(self.frame)):

to:

for i in range(min(len(self.frame), self.max_rows)):

@onesandzeroes
Copy link
Contributor

Can you write up some short example code that will reproduce the issue? Preferably code that's simple and self-contained enough that people can just copy-paste into a Python console to see the issue for themselves.

@pilollipietro
Copy link
Author

Of course:

import pandas as pd
import numpy as np
df = pd.DataFrame({'a': np.arange(10), 'b': np.random.randn(10)})
df.to_html(index=False,max_rows=3)

@pilollipietro pilollipietro reopened this Sep 16, 2014
@onesandzeroes
Copy link
Contributor

Hmmm, I think the fix for this one is actually a little bit more complicated than you've outlined (although you're definitely right to have noticed that max_rows should have been used in there somewhere rather than just using len(self.frame).

The bug you've found only occurs when index=False, and when index=True there's a lot more work going on to print a nice truncated version of the dataframe. None of that truncation seems to happen currently when index=False. Probably need to carefully go over all the things that are being done when index=True and make sure that the same things happen in this case. It gets even more complicated when we have a hierarchical index, so I might try and fix the single index case first and then see where we are.

Here's some extra test code if people want to compare the index=False case to index=True:

import pandas as pd
import numpy as np

df = pd.DataFrame({'a': np.arange(10), 'b': np.random.randn(10)})


print(df.to_html(max_rows=3))
print(df.to_html(index=False, max_rows=3))


# Hierarchical index cases
cols = pd.MultiIndex.from_tuples([('a', 'x'), ('a', 'y'), ('b', 'x'), ('b', 'y')])
index = pd.MultiIndex.from_arrays([
    [1, 1, 1, 1, 1, 2, 2, 2, 2, 2],
    ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'bar']
])
df2 = pd.DataFrame(np.random.randn(10, 4), columns=cols, index=index)
print(df2)
print(df2.to_html(max_rows=3))
print(df2.to_html(index=False, max_rows=3))

@jreback
Copy link
Contributor

jreback commented Oct 20, 2015

closing as stale. pls reopen if still an issue.

@jreback jreback closed this as completed Oct 20, 2015
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 Output-Formatting __repr__ of pandas objects, to_string
Projects
None yet
Development

No branches or pull requests

3 participants