pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
43.3k stars 17.8k forks source link

Pandas Series should provide to_html method #8829

Open scls19fr opened 9 years ago

scls19fr commented 9 years ago

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 https://github.com/pydata/pandas/issues/8825

Kind regards

jreback commented 9 years ago

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

cpcloud commented 9 years ago

@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 commented 9 years ago

@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 commented 9 years ago

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 commented 9 years ago

@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 https://github.com/pydata/pandas/issues/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:]
bsweger commented 7 years ago

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 commented 6 years ago

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 commented 5 years ago

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

WillAyd commented 5 years ago

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

mrocklin commented 5 years ago

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 commented 5 years ago

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

TomAugspurger commented 5 years ago

Not quite, as we need the Series repr to be visually distinct from the equivalent 1-column DataFrame.

Even just placing the Series and Index names at the bottom, rather than the top, of the output table may be sufficient.

On Fri, Mar 8, 2019 at 12:49 PM Benjamin Zaitlen notifications@github.com wrote:

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

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/pandas-dev/pandas/issues/8829#issuecomment-471034935, or mute the thread https://github.com/notifications/unsubscribe-auth/ABQHIpWOwCu3ZYInpdvTAvG4hLwpCSmaks5vUrDWgaJpZM4C7-kN .

mrocklin commented 5 years ago

There is some small amount of work here: https://github.com/pandas-dev/pandas/pull/27228