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

Pandas Series should provide to_html method #8829

Open
scls19fr opened this issue Nov 16, 2014 · 13 comments
Open

Pandas Series should provide to_html method #8829

scls19fr opened this issue Nov 16, 2014 · 13 comments
Labels
Enhancement IO HTML read_html, to_html, Styler.apply, Styler.applymap

Comments

@scls19fr
Copy link
Contributor

Hello,

Pandas Series provide to_json method but they doesn't provide to_html method.
Adding to_html will avoid user to convert a pandas.core.series.Series to a pandas.core.frame.DataFrame in order to output HTML file using s.to_frame().to_html(...) but directly use s.to_html(...)

see also #8825

Kind regards

@jreback
Copy link
Contributor

jreback commented Nov 16, 2014

I'll add to the list. But would be really helpful if you would submit a pull-request to fix.

@jreback jreback added API Design IO HTML read_html, to_html, Styler.apply, Styler.applymap labels Nov 16, 2014
@jreback jreback added this to the 0.16.0 milestone Nov 16, 2014
@jreback jreback added the Compat pandas objects compatability with Numpy or Python functions label Nov 16, 2014
@cpcloud
Copy link
Member

cpcloud commented Nov 16, 2014

@scls19fr Can you give us an example of why this is necessary? Are you converting Series to html on the command line so much that it's inconvenient? Are you calling to_html on things which may be coming in as either DataFrames or Series? The former case isn't convincing to me. The latter is more convincing, but often times when one is getting mixed Series and DataFrame output there's a way to make sure you're always getting the same type of object upstream. IMO that would be the way to solve this problem, since it takes advantage of existing APIs and doesn't require an API addition to fix the problem.

A Series isn't a Table-like thing, it's a vector like thing. As such, making users be explicit about making it a single column table is more readable IMO.

@scls19fr
Copy link
Contributor Author

scls19fr commented Nov 16, 2014

@cpcloud I'm displaying sensor values to a Python Flask app. Values of a sensor are stored in a Series, index is time.
I'm using Datatables JS library http://www.datatables.net/ to provide sorting, filter... functionalities

@cpcloud
Copy link
Member

cpcloud commented Nov 16, 2014

oh ok sounds interesting. what's wrong with calling .to_frame().to_html() then? if you only ever have Series then what's the issue? just too verbose?

@scls19fr
Copy link
Contributor Author

@cpcloud yes quite verbose... that was just my point of view!

But anyway I was also having trouble with to_html method because id parameter is not yet supported

see #8496

I've defined this uggly function

def df_to_html_with_id(df, id, *args, **kwargs):
    s = df.to_html(*args, **kwargs)
    return s[:7] + 'id="%s" ' % id + s[7:]

so I can also do

def ts_to_html_with_id(ts, id, *args, **kwargs):
    s = ts.to_frame().to_html(*args, **kwargs)
    return s[:7] + 'id="%s" ' % id + s[7:]

or mix both using isintance and write something like

def to_html_with_id(param, id, *args, **kwargs):
    if isinstance(param, pd.core.frame.DataFrame):
        s = param.to_html(*args, **kwargs)
    elif isinstance(param, pd.core.series.Series):
        s = param.to_frame().to_html(*args, **kwargs)
    else:
        raise(NotImplementedError)
    return s[:7] + 'id="%s" ' % id + s[7:]

@jreback jreback modified the milestones: 0.16.0, Next Major Release Mar 6, 2015
@bsweger
Copy link
Contributor

bsweger commented May 23, 2017

Adding a note here because I'm working on #16180, which requests a to_latex method on Series, which will enable some desired code cleanup noted in PR #16171

If I'm interpreting correctly, implementing to_html() for a Series is the other thing that needs to happen before said cleanup. Am happy to work on this once to_latex is all squared away (PR #16465)

@dhouck
Copy link

dhouck commented Dec 7, 2017

Currently, a Series provides _repr_latex_ (if you enable the display.latex.repr option), but not _repr_html_. The Jupyter notebook will attempt to render it using MathJax, which doesn’t support the LaTeX used in the representation. This means that in order to get notebooks which export to LaTeX documents with nice tables, you need to either juggle options constantly or sacrifice the ability to read Series in HTML view.

Therefore, even if to_html is kept for DataFrames only as was argued three years ago, it would be nice to have _repr_html_.

@mrocklin
Copy link
Contributor

mrocklin commented Jan 3, 2019

Checking in here. Would a Series._repr_html_ implementation be welcome?

@WillAyd
Copy link
Member

WillAyd commented Jan 3, 2019

I don’t have an objection to to_html for Series since we have that same signature for many other formats

@mrocklin
Copy link
Contributor

mrocklin commented Jan 3, 2019

I suspect that _repr_html_ will be slightly more contentious than to_html because it will change the rendering in Jupyter notebooks, which has a greater impact on user experience.

@quasiben
Copy link

quasiben commented Mar 8, 2019

Can Series._repr_html_ call to Series.to_frame()._repr_html_ ?

@TomAugspurger
Copy link
Contributor

TomAugspurger commented Mar 8, 2019 via email

@mrocklin mrocklin mentioned this issue Jul 4, 2019
4 tasks
@mrocklin
Copy link
Contributor

mrocklin commented Jul 4, 2019

There is some small amount of work here: #27228

@mroeschke mroeschke added Enhancement and removed Compat pandas objects compatability with Numpy or Python functions labels Apr 10, 2020
@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
Luisruizbcn pushed a commit to Luisruizbcn/Onerepositorio that referenced this issue May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement IO HTML read_html, to_html, Styler.apply, Styler.applymap
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants